Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a UIButton while it's animating

I have found one similar question to this but it did not answer my question. I have a UIButton which is animating from the bottom of the screen to the top. I would like to be able to use the button while it is moving. Now, the button can only be used when the animation has finished and the button is no longer animating. Also, I've heard that I might need to use something called NSTimer?

class ViewController: UIViewController {

    @IBAction func button2(sender: UIButton) {
        button.hidden = false
        button.center = CGPointMake(126, 380);        
        UIView.animateKeyframesWithDuration(3, delay: 0, options:   .AllowUserInteraction,
            animations: { () -> Void in
            self.button.center = CGPointMake(126, 130 )
            }) { (_) -> Void in
        }   
    }   

    @IBOutlet var label: UILabel! 
    @IBOutlet var button: UIButton!

    @IBAction func button1(sender: UIButton) {
        button.hidden = true
        label.hidden = false
    }   

    override func viewDidLoad() {
        super.viewDidLoad() 
        button.hidden = true
        label.hidden = true
    } 
}
like image 396
Anton O. Avatar asked Dec 05 '15 02:12

Anton O.


People also ask

How can I check if UIButton is pressed?

If you want to check If UIButton is Pressed or not, You should handle TouchDown Handler and change button's state to pressed in touchDown hadnler. You can track ToucUpInside method to Change state of button to Not-pressed again.

How does UIView animate work?

it queries the views objects for changed state and gets back the initial and target values and hence knows what properties to change and in what range to perform the changes. it calculates the intermediate frames, based on the duration and initial/target values, and fires the animation.

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] . It's best to let the OS run your animations; the animation thread does not block the main thread -- only the animation block itself.

Is UI animation right for your user interface?

While it’s easy to get carried away by UI animation’s enticing creative possibilities, keep in mind that successfully incorporating animation into a user interface ultimately hinges on usability. It’s down to the UI designer to carefully consider the value of each animation—no matter how subtle, or eye-catching—and what it means for the end-user.

How many lessons does it take to learn UI animation?

This UI Animation Essentials Course will take you from total beginner to animation pro, with 12 structured online lessons. For more UI portfolio inspiration, take a look at this round-up of awesome UI portfolios. 7. Final thoughts So there we have it: everything you need to know to get started in UI animation.

What is UI motion graphics?

Motion graphics, on the other hand, refers to the process of adding motion to graphic design elements. Put simply, motion graphics are a type of animation. Unlike other types of animation, motion graphics tend to place less emphasis on storytelling. To learn more, check out these examples of UI animation. 2. Why is UI animation important?

How do I get Started with UI design?

Get a hands-on introduction to UI design and design your very first app screen with a free, self-paced UI Design Short Course. Take part in one of our FREE live online UI design events with industry experts.


1 Answers

You have to use a CADisplayLink. For example:

@IBOutlet var button2: UIButton!

@IBAction func button3(sender: UIButton)
{
    label.hidden = false
    button2.hidden = true
}

@IBAction func button1(sender: UIButton)
{
    button2.frame = CGRectMake(120, 400, 100, 100)
    let displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
    displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode:   NSDefaultRunLoopMode)
}

func handleDisplayLink(displayLink: CADisplayLink)
{
    var buttonFrame = button2.frame
    buttonFrame.origin.y += -2
    button2.frame = buttonFrame

    if button2.frame.origin.y <= 50 
    {
        displayLink.invalidate()
    }
}

You can also check this question: Moving a button in swift using animate with duration with constraints and detecting a touch during it

like image 149
Anton O. Avatar answered Sep 28 '22 06:09

Anton O.