UIGesture 手勢

UIGesture 手勢


ViewController.h

//協議 - 實現多個手勢同時操作
<UIGestureRecognizerDelegate>

//定義視圖對象
UIImageView *_imageView;

//捏合手勢,可以對視圖放大縮小
UIPinchGestureRecognizer *_pinchGes;

//定義 旋轉手勢,旋轉圖片
UIRotationGestureRecognizer *_rotGes;

//平移 手勢
UIPanGestureRecognizer *_pan;

//長按 手勢
UISwipeGestureRecognizer *_swipe;

ViewController.m

//加載圖像對象,從本地加載到內存中
UIImage *image = [UIImage imageNamed:@"123"];

//創建圖像視圖
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(34, 164, 306, 217)];

//將圖像視圖賦值
_imageView.image = image;

//按比例縮放。是啥樣就是啥樣,保持原來的樣子
//    _imageView.contentMode = UIViewContentModeScaleAspectFit;

[self.view addSubview:_imageView];

//交互事件響應開關
//YES:可以交互,必須要設置
//NO:不能接收交互。默認是NO
_imageView.userInteractionEnabled = YES;

#pragma mark - 單擊放大
//創建點擊手勢對象 self:響應事件的擁有者,self表示當前視圖控制器  action: 響應事件的函數
UITapGestureRecognizer *tapOneGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOneAct:)];

//點擊幾次,纔會觸發   默認是1
tapOneGes.numberOfTapsRequired = 1;
//幾個手指,點擊觸發   默認是1
tapOneGes.numberOfTouchesRequired = 1;

//將點擊事件添加到視圖中,視圖即可響應事件
[_imageView addGestureRecognizer:tapOneGes];


//單擊放大響應函數
//參數手勢點擊事件對象
- (void)tapOneAct:(UITapGestureRecognizer *)tap
{
    //隨機背景顏色
    NSLog(@"放大");
//    float randomR = arc4random_uniform(255)/255.0;
//    float randomG = arc4random_uniform(255)/255.0;
//    float randomB = arc4random_uniform(255)/255.0;
//    
//    UIColor *randomColor = [UIColor colorWithRed:randomR   green:randomG blue:randomB alpha:1];
//    
//    self.view.backgroundColor = randomColor;

單擊隨機顏色.gif
    //獲取手勢監控的視圖對象
    UIImageView* imageView = (UIImageView*) tap.view;

    //開始動畫過程
    [UIView beginAnimations:nil context:nil];

    //設置動畫過渡時間
    [UIView setAnimationDuration:2];
    //縮放佈滿整個屏幕。  iPhone6  inch-4.7
    imageView.frame = CGRectMake(0, 0, 375, 667);

    //結束動畫過程
    [UIView commitAnimations];

}

單擊放大.gif
#pragma mark - 雙擊縮小
UITapGestureRecognizer *tapTwoGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwoAct:)];
tapTwoGes.numberOfTapsRequired = 2;
tapTwoGes.numberOfTouchesRequired = 1;
[_imageView addGestureRecognizer:tapTwoGes];

#pragma bug 在原圖情況下,會先放大,在縮小。從打印方式可以看出,雙擊會先出現放大,後出現縮小。  
# 解決方案 ↓
//當單擊操作遇到雙擊操作時,單擊操作失效
[tapOneGes requireGestureRecognizerToFail:tapTwoGes];


//雙擊縮小事件
- (void)tapTwoAct:(UITapGestureRecognizer *)tap
{
    NSLog(@"縮小");

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2];
    _imageView.frame = CGRectMake(34, 164, 306, 217);
    [UIView commitAnimations];

}

(先單擊放大)雙擊縮小.gif
#pragma mark - 捏合 放大縮小
_pinchGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAct:)];

//將捏合手勢添加到視圖上去
[_imageView addGestureRecognizer:_pinchGes];


//捏合-放大縮小事件  按住alt鍵
- (void)pinchAct:(UIPinchGestureRecognizer *)pinch
{
    NSLog(@"如果我有仙女棒,變大變小變漂亮");
    //對圖像視圖進行矩陣變換計算並賦值
    /*
     transform 圖形學中的變換矩陣
     CGAffineTransformScale 通過縮放的方式產生一個新的矩陣
     參數1 : 原來的矩陣
     參數2 : x方向的縮放比例
     參數3 : y方向的縮放比例
     返回值是新的縮放後的矩陣變換
     */
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    //將縮放值歸爲單位值,否則會出現累加
    /*
     scale = 1 原來的大小
     scale > 1 放大效果
     scale < 1 縮小效果
     */
    pinch.scale = 1;
}

捏合事件.gif
#pragma mark - 旋轉
_rotGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotAct:)];

[_imageView addGestureRecognizer:_rotGes];


//旋轉事件
- (void)rotAct:(UIRotationGestureRecognizer *)rot
{
     NSLog(@"看我歪頭");
    rot.view.transform =   CGAffineTransformRotate(rot.view.transform, rot.rotation);
    //選擇角度清零
    rot.rotation = 0;
}

旋轉.gif
# 協議 - 同時識別手勢
_rotGes.delegate = self;
_pinchGes.delegate = self;


//Simultaneously 同時識別手勢 YES: 可以同時相應,NO: 不可以
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

同時識別旋轉放大縮小.gif
#pragma mark - 移動
_pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAct:)];

[_imageView addGestureRecognizer:_pan];

#這裏,爲了避免和平掃衝突,所以取消掉移動的事件。
//將移動事件從圖像視圖中取消
//[_imageView removeGestureRecognizer:_pan];


//平移 事件  只要手指發生變化,函數就被調用
- (void)panAct:(UIPanGestureRecognizer *)pan
{
    //    NSLog(@"111111111111");
    //    //獲取移動的座標,相對於視圖的座標而言
    //    CGPoint pt = [pan translationInView:self.view];
    //    NSLog(@"pt.x = %.2f , pt.y = %.2f",pt.x,pt.y);
    //
    //    //獲取移動時的 相對速度  每秒在屏幕移動像素的值
    //    CGPoint pv = [pan velocityInView:self.view];
    //    NSLog(@"pv.x = %.2f , pv.y = %.2f",pv.x,pv.y);

    //在pan.view上移動的距離
    CGPoint translation = [pan translationInView:pan.view];
    //view的中心
    CGPoint center = pan.view.center;
    //中心點.x + 移動距離.x
    center.x = center.x + translation.x;
    center.y = center.y + translation.y;
    pan.view.center = center;
    //清空移動距離
    [pan setTranslation:CGPointZero inView:pan.view];

}

移動.gif
#pragma mark - 滑動(平掃)
_swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAct:)];
/*
 UISwipeGestureRecognizerDirection

 Right  向右
 Left   向左
 Up     向上
 Down   向下
 */
//  | 位或
_swipe.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;

[_imageView addGestureRecognizer:_swipe];


//滑動 事件
- (void)swipeAct:(UISwipeGestureRecognizer *)swipe
{
    //    NSLog(@"向左滑動");
#pragma mark - 有問題 ,不能分別打印
    if (_swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"向左滑動");
    }else{
        NSLog(@"向右滑動");
    }
}

打印出向右滑動
#pragma mark - 長按
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pressLong:)];

//設置長按手勢的時間,默認是0.5
press.minimumPressDuration = 3;

[_imageView addGestureRecognizer:press];


//長按事件
- (void)pressLong:(UILongPressGestureRecognizer *)press
{
    //手勢的狀態對象,到達規定時間(3s),觸發函數
    if (press.state == UIGestureRecognizerStateBegan) {
        NSLog(@"開始!");
    }
    //當手指離開時,結束
    else if (press.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"結束!");
    }
    NSLog(@"長按");
}

長按事件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章