UIGesture手勢高級:UIPinchGestureRecognizer捏合手勢、UIRotationGestureRecognizer旋轉(zhuǎn)手勢

UIPinchGestureRecognizer捏合手勢

  • alloc
  • initWithTarget
  • imageView.transform = CGAffineTransformScale(iView.transform, pinch.scale, pinch.scale);
    // 對圖像視圖對象進(jìn)行矩陣變換計算并賦值
    // CGAffineTransformScale通過縮放的方式產(chǎn)生一個新的矩陣
    //P1:原來的矩陣
    //P2:x方向上的縮放比例
    //P3:y方向上的縮放比例
  • <UIGestureRecognizerDelegate>協(xié)議方法
  • shouldRecognizeSimultaneouslyWithGestureRecognizer是否可以同時響應(yīng)兩種手勢

UIRotationGestureRecognizer旋轉(zhuǎn)手勢

  • alloc
  • initWithTarget
  • imageView.transform = CGAffineTransformRotate(iView.transform, rot.rotation);

具體使用:讓一個圖像視圖支持縮放和旋轉(zhuǎn)手勢
主要代碼如下

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIImage* image = [UIImage imageNamed:@"icon1"];
    UIImageView* iView = [[UIImageView alloc]initWithImage:image];
    iView.frame = CGRectMake(100, 200, 150, 150);
    //打開圖像視圖支持交互的開關(guān)
    iView.userInteractionEnabled = YES;
    [self.view addSubview:iView];
    //創(chuàng)建捏合手勢對象
    _pinchGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAct:)];
    [iView addGestureRecognizer:_pinchGes];
    //創(chuàng)建旋轉(zhuǎn)手勢
    _rotGes = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotAct:)];
    [iView addGestureRecognizer:_rotGes];
    
    _rotGes.delegate = self;
    _pinchGes.delegate = self;
}
//協(xié)議方法:是否可以同時響應(yīng)兩個手勢
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
-(void)rotAct:(UIRotationGestureRecognizer *)rot{
    UIImageView* iView = (UIImageView *)rot.view;
    //計算旋轉(zhuǎn)的變換矩陣并且賦值
    iView.transform = CGAffineTransformRotate(iView.transform, rot.rotation);
    //旋轉(zhuǎn)角度清零
    rot.rotation = 0;
}

-(void)pinchAct:(UIPinchGestureRecognizer *)pinch{
    //獲取監(jiān)控視圖圖像
    UIImageView* iView = (UIImageView *)pinch.view;
    //對圖像視圖對象進(jìn)行矩陣變換計算并賦值
    //CGAffineTransformScale通過縮放的方式產(chǎn)生一個新的矩陣
    //P1:原來的矩陣
    //P2:x方向上的縮放比例
    //P3:y方向上的縮放比例
    iView.transform = CGAffineTransformScale(iView.transform, pinch.scale, pinch.scale);
    
    //將縮放值歸位為單位值,由于響應(yīng)函數(shù)是在每個瞬間都調(diào)用的(不管手指是否有滑動)
    //如果不歸零,縮放后保持手指觸屏位置不變時,圖片也會一直按照上一次縮放的比例繼續(xù)縮放。
    //scale=1原來的大小
    pinch.scale = 1;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容