iOS開發之常用六種手勢
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//單擊
UITapGestureRecognizer *click = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(Click:)];
[self.viewaddGestureRecognizer:click];
//雙擊
UITapGestureRecognizer *doubleClick = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(DoubleClick:)];
doubleClick.numberOfTapsRequired = 2;
[click requireGestureRecognizerToFail:doubleClick];//取消單擊方法
[self.viewaddGestureRecognizer:doubleClick];
//滑動
UISwipeGestureRecognizer *swipe = [[
swipe.direction =UISwipeGestureRecognizerDirectionRight;
[self.viewaddGestureRecognizer:swipe];
//長按
UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(100,100, 50, 50)];
button.backgroundColor = [UIColor
[button addTarget:selfaction:@selector(ClickButton:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(LongPress:)];
longPress.minimumPressDuration =2.0;
[button addGestureRecognizer:longPress];
//平移
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(Pan:)];
[self.viewaddGestureRecognizer:pan];
//旋轉
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(Rotation:)];
[self.viewaddGestureRecognizer:rotation];
}
-(IBAction)Click:(id)sender{
NSLog(@"Click.");
}
-(IBAction)DoubleClick:(id)sender{
NSLog(@"DoubleClick.");
}
-(IBAction)Swipe:(id)sender{
NSLog(@"Swipe.");
}
-(IBAction)ClickButton:(id)sender{
NSLog(@"ClickButton.");
}
//不同的操作邏輯會出現不同的結果
-(IBAction)LongPress:(UILongPressGestureRecognizer*)sender{
sender.allowableMovement =25.0;
if (sender.state==UIGestureRecognizerStateChanged) {
NSLog(@"StateChanged.");
}elseif (sender.state==UIGestureRecognizerStateEnded){
NSLog(@"LongPress.");
}
}
-(IBAction)Pan:(UIPanGestureRecognizer*)sender{
NSLog(@"Pan.");
}
-(IBAction)Rotation:(UIRotationGestureRecognizer*)sender{
NSLog(@"Rotation.");
NSLog(@"%f",sender.rotation*(180/M_PI));
}
@end