Moving UIImage Up and Down with Animation in Swift - ios

I have just recently started with Xcode and Swift and I am trying to get my image to move up and down and have it switch directions when the user taps the screen. My image just will not move at all and the app will either crash or just freeze.
import UIKit
import SpriteKit
import Darwin
let startButton = UIButton(frame: CGRect(x: 200, y: 100, width: 100, height: 50))
let screenSize = UIScreen.main.bounds
let gameButton = UIButton(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))
var dot: UIImage = UIImage(named: "dot")!
var dotPic = UIImageView(image: dot)
public var goingUp = true
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
startButton.addTarget(self, action: #selector(startButtonAction), for: .touchUpInside)
startButton.setImage(UIImage.init(named: "startButton"), for: .normal)
self.view.addSubview(startButton)
}
func startButtonAction(sender: UIButton!) {
print("button clicked")
startButton.isEnabled = false
startButton.setImage(UIImage.init(named: "nothing"), for: .normal)
self.view.backgroundColor = .gray
dotPic.frame = CGRect(x: 50, y: 300, width: 20, height: 20)
self.view.addSubview(dotPic)
self.view.addSubview(gameButton)
playGame()
}
func playGame(){
gameButton.addTarget(self, action: #selector(gameButtonAction), for: .touchUpInside)
gameButton.setImage(UIImage.init(named: "nothing"), for: .normal)
}
func gameButtonAction(sender: UIButton!) {
goingUp = !goingUp
print(goingUp)
print("clicked up/down")
func rep(){
repeat{
dotPic.frame = CGRect(x: 50, y: 300-1, width: 20, height: 20)
sleep(UInt32(0.3))
}while goingUp==true
repeat{
dotPic.frame = CGRect(x: 50, y: 300-1, width: 20, height: 20)
sleep(UInt32(0.3))
}while goingUp==false
}
rep()
}
}
I have tried using animateWithDuration() too but it has always frozen the app. What am I doing wrong?

Tried to use this code in swift...
self.hintImageView.frame = CGRect(x: x, y: 55, width: 200, height: 40)
UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat, .autoreverse] , animations: {
self.hintImageView.frame = CGRect(x: x, y: 65, width: 200, height: 40);
}) { (completed) in
}

Here, Try this, instead of func rep() in your gameButtonAction :
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
//code with animation
dotPic.frame = CGRect(x: 50, y: 300-1, width: 20, height: 20)
} completion:^(BOOL finished) {
//code for completion
dotPic.frame = CGRect(x: 50, y: 300, width: 20, height: 20)
}];
and avoid using nested function rep() in your target. Probably it is running continuously causing your application to hang.

Related

Indent UITextField text and placeholder

I have programmatically created a UITextField in my UITableView footer like so:
let customView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 50))
self.textArea = UITextField(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width - 50, height: 50))
self.textArea.placeholder = "Add Item"
self.textArea.layer.borderColor = UIColor.gray.cgColor
self.textArea.layer.borderWidth = 1
customView.addSubview(self.textArea)
let button = UIButton(frame: CGRect(x: self.view.bounds.width - 50, y: 0, width: 50, height: 50))
button.setTitle("Add", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = UIColor(displayP3Red: 3.0 / 255.0, green: 0.0 / 255.0, blue: 113.0 / 255.0, alpha: 1.0)
button.addTarget(self, action: #selector(self.addWishListItem), for: .touchUpInside)
customView.addSubview(button)
self.tableView.tableFooterView = customView
My question is, how do I indent the UITextField text and placeholder text so it's not right at the edge of the left side?
I found this:
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: self.textArea.frame.height))
self.textArea.leftView = paddingView
self.textArea.leftViewMode = .always
Is there a better way on doing this?
You can create a customTextField and override the rects for text, placeholder, border.. basically everything.
import UIKit
class CustomTextField: UITextField {
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return CGRect.init(x: 10, y: -10, width: bounds.width, height: bounds.height)
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
return CGRect.init(x: 10, y: -10, width: bounds.width, height: bounds.height)
}
}
You control the position of it by changing x and y values

How to add a static header in JSQMessageView Controller

How i can add header just like the image below which should not scroll up with view controller.
To add static header view, use following function (Swift 4):
Note: I have tried to put this kind of header view directly using storyboard. But, it wont worked. So, finally I achieved it by adding programatically.
func addHeaderView() {
let selectableView = UIView(frame: CGRect(x: 0, y: 20, width: self.view.bounds.width, height: 60))
selectableView.backgroundColor = UIColor(red: 56.0/255.0, green: 120.0/255.0, blue: 222.0/255.0, alpha: 1.0)
let btn: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
btn.setImage(UIImage(named:"ArrowBack"), for: .normal)
btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btn.tag = 1
selectableView.addSubview(btn)
let titleLabel = UILabel(frame: CGRect(x: btn.frame.size.width+5, y: 5, width: selectableView.frame.size.width-60, height: 18))
titleLabel.textColor = UIColor.white
titleLabel.text = "Winter event 2017"
selectableView.addSubview(titleLabel)
let subTitleLabel = UILabel(frame: CGRect(x: btn.frame.size.width+5, y: 30, width: 100, height: 16))
subTitleLabel.textColor = UIColor.white
subTitleLabel.text = "135 members"
selectableView.addSubview(subTitleLabel)
let btnOptions: UIButton = UIButton(frame: CGRect(x: selectableView.frame.size.width-60, y: 0, width: 50, height: 50))
btnOptions.setImage(UIImage(named:"Settings"), for: .normal)
btnOptions.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btnOptions.tag = 2
selectableView.addSubview(btnOptions)
view.addSubview(selectableView)
}
//then make a action method :
#objc func buttonAction(sender: UIButton!) {
let btnsendtag: UIButton = sender
if btnsendtag.tag == 1 {
self.navigationController?.popViewController(animated: true)
} else {
askForSettings()
}
}
Also put this line of code in viewDidLoad()
self.collectionView?.collectionViewLayout.sectionInset = UIEdgeInsets(top: 80, left: 0, bottom: 0, right: 0)

How to animate UIButton frame from left to right and reverse

My current method is to add a view to the button as subview:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 0, height: 60))
button.setTitle("Add", for: .normal)
buttonView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 60))
buttonView.backgroundColor = .black
button.addSubview(buttonView)
And then animate the width of the view:
UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseIn, animations: {
self.buttonView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60)
}, completion: nil)
What I don't like about this is that the button title remains static. I want it to be part of the animation.
I also try to animate the frame of the button instead, but it doesn't seem to work. Nothing happens.
My button is added to the UITextField inputAccessoryView so I am not sure if this is the problem.
Any suggestions how can I animate the button correctly ?
You don't need an additional subview for UIButton in order to animate it's frame.
Try to add layoutIfNeeded() before animation and inside animation closure:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
button.setTitle("Add", for: .normal)
button.setTitleColor(UIColor.green, for: .normal)
button.backgroundColor = UIColor.black
view.addSubview(button)
button.layoutIfNeeded()
UIView.animate(withDuration: 3, delay: 0.0, options: .curveEaseIn, animations: {
button.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60)
button.layoutIfNeeded()
}, completion: nil)
Hope this helps.

Swift - If statement not working as it should

I'm trying to animate an UIButton once two textsfields have had text entered. The button animates even if there is no text entered. I thought maybe it was an issue with there being placeholder text, but that has no effect. I have tried != "" as well as != nil
Here's my code
override func viewDidLoad() {
super.viewDidLoad()
self.title = ""
textFieldConfig()
loginButtonConfig("LOG IN")
}
func loginButtonConfig(title:String) {
let frameWidth = self.view.frame.width
let frameHeight = self.view.frame.height
var button = UIButton()
button.frame = CGRect(x: 0, y: 300, width: 0, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: 0, height: 50)
button.backgroundColor = UIColor(red: 24.0/255.0, green: 198.0/255.0, blue: 152.0/255.0, alpha: 1.0)
button.setTitle(title, forState: .Normal)
button.titleLabel?.textAlignment = NSTextAlignment.Center
self.view.addSubview(button)
//WHY ISN'T THE IF STATEMENT WORKING AS IT SHOULD???
if self.userTextField.text != nil && self.passwordTextField.text != nil {
UIView.animateWithDuration(1.0, animations: { () -> Void in
button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
})
}
}
Any help would be appreciated. Thanks :)
Try this:
if !self.userTextField.text.isEmpty && !self.passwordTextField.isEmpty {
UIView.animateWithDuration(1.0, animations: { () -> Void in
button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
})
}
Try this:
if self.userTextField.text! != "" && self.passwordTextField.text! != "" {
UIView.animateWithDuration(1.0, animations: { () -> Void in
button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
})
}

Remove subview created for activityIndicator with swift

i have created an activity window function which takes 3 inputs,
msg:String - the message which should show in the activity window
indicator:Bool - if the activity window should show or not
view:UIView - the uiview which should get the frame sizes etc from the View Controller which its called on
everything works fine, except the part where subview needs to be removed.if the same function is run on the main view controller. it works fine. just when i moved it to NSObject, it does not. please help
class UIDesignFunction: NSObject {
func progressBarDisplayer(msg:String, indicator:Bool, view:UIView) {
var activityIndicator = UIActivityIndicatorView()
var strLabel = UILabel()
var msgFrame = UIView()
if indicator{
//println(msg)
strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
strLabel.text = msg
strLabel.textColor = UIColor.whiteColor()
msgFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))
msgFrame.layer.cornerRadius = 15
msgFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityIndicator.startAnimating()
msgFrame.addSubview(activityIndicator)
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
msgFrame.addSubview(strLabel)
view.addSubview(msgFrame)
}else{
UIApplication.sharedApplication().endIgnoringInteractionEvents()
msgFrame.removeFromSuperview()
}
}
move the variables out of the function like below
class UIDesignFunction: NSObject {
var activityIndicator = UIActivityIndicatorView()
var strLabel = UILabel()
var msgFrame = UIView()
func progressBarDisplayer(msg:String, indicator:Bool, view:UIView) {
if indicator{
//println(msg)
strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
strLabel.text = msg
strLabel.textColor = UIColor.whiteColor()
msgFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))
msgFrame.layer.cornerRadius = 15
msgFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityIndicator.startAnimating()
msgFrame.addSubview(activityIndicator)
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
msgFrame.addSubview(strLabel)
view.addSubview(msgFrame)
}else{
UIApplication.sharedApplication().endIgnoringInteractionEvents()
msgFrame.removeFromSuperview()
}
}

Resources