Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macRumorsUserX

macrumors newbie
Original poster
Feb 2, 2015
8
0
What is the easiest way (in Swift) to create a function that triggers a UI animation when one touches a view and another animation when you stop touching that view, so this is what i am trying to write.

Code:
func TapView1(){

//touching View1
            UIView.animateWithDuration(1.0, animations: {
            View1.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
            })

//stops touching View1
            UIView.animateWithDuration(1.0, animations: {
            View1.frame = CGRect(x: 50, y: 50, width: 50, height: 50)
            })
        }
 
Last edited by a moderator:
What is the easiest way (in Swift) to create a function that triggers a UI animation when one touches a view and another animation when you stop touching that view, so this is what i am trying to write.

Code:
func TapView1(){

//touching View1
            UIView.animateWithDuration(1.0, animations: {
            View1.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
            })

//stops touching View1
            UIView.animateWithDuration(1.0, animations: {
            View1.frame = CGRect(x: 50, y: 50, width: 50, height: 50)
            })
        }

You should read up on UIGestureRecognizer (and more importantly it's subclasses like UILongPressGestureRecognizer & UIPanGestureRecognizer which might be useful here).

If you read through the docs you'll see that the UIGestureRecognizer class has a state, which can vary from various values like "Began" (When the user initiated the gesture), "Changed" and "Ended" - all key points for you to apply your respective animations.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.