1.輕拍手勢
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
[myView addGestureRecognizer:tap];
[tap release];
tap.numberOfTapsRequired = 2; ?// 設(shè)置點擊次數(shù)(雙擊)
tap.numberOfTouchesRequired = 2; // 設(shè)置需要手指數(shù)
2.輕掃手勢
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe)];
[myView addGestureRecognizer:swipe];
[swipe release];
swipe.direction = UISwipeGestureRecognizerDirectionUp; // 設(shè)置輕掃方向
3.長按手勢
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress)];
[myView addGestureRecognizer:longPress];
[longPress release];
longPress.minimumPressDuration = 2; ? // 設(shè)置長按時間
4.平移手勢
UIPanGestureRecognizer*pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan)];
[myView addGestureRecognizer:pan];
[pan release];
- (void)panAction:(UIPanGestureRecognizer *)pan{
CGPointpoint = [pan translationInView:pan.view];
// transform是視圖的一個重要屬性我們的平移旋轉(zhuǎn)縮放改變的都是視圖的transform屬性我們用何種方法去改變他取決于賦值符號右邊調(diào)用的api
//我們之后學習的動畫有很多API都是跟transform有關(guān)的
//平移手勢在做這個動作時相當于實時地觸發(fā)這個方法我們每一次的平移都會在上一次平移的基礎(chǔ)上繼續(xù)平移相當于每一次新的值都會再次變?yōu)槔系闹?/p>
pan.view.transform=CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
[pan setTranslation:CGPointZero inView:pan.view]; //重新置成0點
}
5.縮放手勢
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch)];
[myView addGestureRecognizer:pinch];
[pinch release];
-(void)pinch:(UIPinchGestureRecognizer*)pinch{
pinch.view.transform=CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
pinch.scale = 1;
}
6.旋轉(zhuǎn)手勢
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation)];
[myView addGestureRecognizer:rotation];
[rotation release];
-(void)rotation:(UIRotationGestureRecognizer*)rotation{
//下一次的旋轉(zhuǎn)都會根據(jù)上一次的旋轉(zhuǎn)繼續(xù)旋轉(zhuǎn)
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
rotation.rotation=0;
}
手勢部分的筆記:?
? ? ? ? UIImageView是用來展示圖片的 ? 系統(tǒng)沒有給我們提供addTarget action方法所以我們想讓UIImageView可以點擊需要給其加手勢
? ? ? ? ?手勢一根分為七大種每一個手勢的類都是繼承于UIGestureRecognizer我們操作的時候是操作其子類(邊界手勢本文沒有寫)
? ? ? ? ?一個手勢只能添加到一個視圖上如果添加到多個視圖上只是最后添加的那個視圖才會擁有這個手勢
? ? ? ? 當我們想要操作ImageView時? ? 需要將其交互打開(默認是關(guān)閉的)? 交互打開的代碼如下imageView.userInteractionEnabled = YES;