------視圖控制器的.m中------------
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *gestureView = [[UIView alloc]initWithFrame:CGRectMake(20, 50, 335, 400)];
gestureView.backgroundColor = [UIColor purpleColor];
[self.view addSubview:gestureView];
/**********************點擊手勢***************************/
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap1Action:)];
//一個點擊手勢,只能識別一種手勢,單擊和雙擊是兩種不同的手勢
//點擊次數
tap1.numberOfTapsRequired = 1;
//點擊個數
tap1.numberOfTouchesRequired =1;
//往gestureView張添加手勢
[gestureView addGestureRecognizer:tap1];
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap2Action:)];
tap2.numberOfTapsRequired = 2;
[gestureView addGestureRecognizer:tap2];
//如果參數中的手勢觸發了,則自身失效
[tap1 requireGestureRecognizerToFail:tap2];
/**********************輕掃手勢**************************/
UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
//設置方向
swip.direction = UISwipeGestureRecognizerDirectionLeft;
[gestureView addGestureRecognizer:swip];
/**********************平移手勢**************************/
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[gestureView addGestureRecognizer:pan];
/**********************長按手勢**************************/
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
//設置長按的最短時間
longPress.minimumPressDuration = 2;
[gestureView addGestureRecognizer:longPress];
/**********************旋轉手勢**************************/
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[gestureView addGestureRecognizer:rotation];
/**********************捏合手勢**************************/
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[gestureView addGestureRecognizer:pinch];
}
- (void)tap1Action:(UITapGestureRecognizer *)tap1{
NSLog(@"單擊");
}
- (void)tap2Action:(UITapGestureRecognizer *)tap2{
NSLog(@"雙擊");
}
- (void)swipAction:(UISwipeGestureRecognizer *)swip{
if (swip.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"向左輕掃");
}
}
- (void)panAction:(UIPanGestureRecognizer *)pan{
CGPoint point = [pan locationInView:pan.view];
NSLog(@"%@",NSStringFromCGPoint(point));
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress{
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"長按開始");
}else if (longPress.state == UIGestureRecognizerStateEnded){
NSLog(@"長按結束");
}
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation{
//獲取旋轉的角度-----先得到弧度
CGFloat r = rotation.rotation;
//轉成角度
//180/PI = x/r
NSLog(@"x is %.2f",180/M_PI*r);
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch{
//獲得捏合的比例
CGFloat scale = pinch.scale;
pinch.view.transform = CGAffineTransformMakeScale(scale, scale);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。