iPhone View Sliding effect - Beginner
What is the event in iPhone development to write code to a slide left and slide right operation.
As in when you slide the view of your phone, you go into another view. What is the event that i should write 开发者_JS百科my code ?
The event you are looking to catch is called a swipe
. Apple provides a UISwipeGestureRecognizer
class to handle this for you. Implementation advice can be found in the Gesture Recognizers section of the Event Handling Guide for iOS.
Briefly, you create the recognizer:
UISwipeGestureRecognizer *swipeGestureRecognizer =
[[[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(swipe:)] autorelease];
Then set how many fingers the swipe is, and what direction it needs to be in:
swipeGestureRecognizer.numberOfTouchesRequired = 2; // default is 1
// allow swiping either left or right, default is right only
swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft |
UISwipeGestureRecognizerDirectionRight;
And then add it to your view:
[self.view addGestureRecognizer:swipeGestureRecognizer];
When the user swipes, your swipe:
method will get called.
Darvids0n covered a gesture to slide the view out. I interpreted the question as an animation to slide the view out, in which case you'll want UIView's transitionFromView:toView:duration:options:completion:
class method:
[UIView transitionFromView:view1 toView:view2 duration:1.0 options:(UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionFlipFromLeft) completion:NULL];
This actually gives you a curl. If you want a slide, you'll have to code a custom animation. Check out UIView's documentation and especially the 'animateWithDuration:animations:' class method.
精彩评论