iOS手勢事件

什么是手勢?

用過蘋果手機(jī)的朋友都知道,我們的屏幕上有個"小圓點","小圓點"當(dāng)你對它做出不同動作的時候會有不同的響應(yīng)事件,拖拽平移來移動它的位置,點擊實現(xiàn)各種按鍵功能的實現(xiàn),這樣大大為用戶提供了便利,并且可以有效地保護(hù)按鍵.

那么類似這種手勢事件在我們的開發(fā)中是怎么實現(xiàn)的?

UIGestureRecognizer

UIKit中包含了UIGestureRecognizer類,用于檢測發(fā)生在設(shè)備中的手勢.UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行為,我們在開發(fā)中使用的是UIGestureRecognizer的子類:
UITapGestureRecognizer

    UIPinchGestureRecognizer

    UIRotationGestureRecognizer

    UISwipeGestureRecognizer

    UIPanGestureRecognizer

    UILongPressGestureRecognizer

見名知意,Tap(輕拍),Pinch(捏合),Rotation(旋轉(zhuǎn)),Swipe(滑動,快速移動,是用于監(jiān)測滑動的方向的),Pan(拖移,慢速移動,是用于監(jiān)測偏移的量的),以及LongPress(長按)

下面來介紹一下這些手勢最基本的使用方法:

@interface RootViewController ()
@property(nonatomic,strong)UIView *greenView;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

self.view.backgroundColor =[ UIColor whiteColor];
    
    self.greenView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    _greenView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_greenView];
    
#pragma mark ---tap手勢
    
    //創(chuàng)建手勢,并初始化,使用initWithTarget: action:方法創(chuàng)建
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
    
    //設(shè)置屬性
    tap.numberOfTapsRequired = 2;//次數(shù)
    tap.numberOfTouchesRequired = 2;//手指數(shù)
    
    //添加到視圖上
//    [_greenView addGestureRecognizer:tap];
    
#pragma mark ----swipe手勢
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)];
    //屬性
    swipe.direction = 0;
    
    
//    [_greenView addGestureRecognizer:swipe];
    
#pragma mark ----longPress手勢
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressView:)];
    
    longPress.minimumPressDuration = 2;
    
//    [_greenView addGestureRecognizer:longPress];
    
#pragma mark ----- pan平移手勢
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
//    [_greenView addGestureRecognizer:pan];
    
    #pragma mark  ----- pinch捏合手勢
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
//    [_greenView addGestureRecognizer:pinch];
    
#pragma mark ----- Rotation旋轉(zhuǎn)手勢
    
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationView:)];
//    [_greenView addGestureRecognizer:rotation];

}

//旋轉(zhuǎn)手勢事件
-(void)rotationView:(UIRotationGestureRecognizer *)sender{
    
    //rotation 旋轉(zhuǎn)角度
    
    sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
    
    //清除增量
    sender.rotation = 0.0;
    
}

//平移手勢事件
-(void)panView:(UIPanGestureRecognizer *)sender{
    
    CGPoint point = [sender translationInView:_greenView];
//    sender.view.transform = CGAffineTransformMake(1, 0, 0, 1, point.x, point.y);
    
    //每次移動都是從原來的位置移動
//    sender.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
    
    //以上次的位置為標(biāo)準(zhǔn)
    sender.view.transform = CGAffineTransformTranslate(sender.view.transform, point.x, point.y);
    //增量置為0
    [sender setTranslation:CGPointZero inView:_greenView];
}

//長按手勢事件
-(void)longPressView:(UILongPressGestureRecognizer *)sender{
    
    
    if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"長按狀態(tài)!");
    }    
    
}

//捏合手勢事件
-(void)pinchView:(UIPinchGestureRecognizer *)sender{
    
    //以上一次所方位標(biāo)準(zhǔn)
    sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
    
    //重新設(shè)置縮放比例,1是正??s放,小于1是縮小,大于1是放大
    sender.scale = 1;
}


-(void)swipeView:(UISwipeGestureRecognizer *)sender{
    //randomColor是自己寫的一個UIColor的類目方法(隨機(jī)色)
    _greenView.backgroundColor = [UIColor randomColor];
}

-(void)tapView:(UITapGestureRecognizer *)sender{
    
    _greenView.backgroundColor = [UIColor randomColor];
}

這就是開發(fā)中最基本的手勢用法,大家都會了嗎?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容