- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
// 1.創(chuàng)建一個(gè)UIImage
// 通過文件名直接進(jìn)行創(chuàng)建
// UIImage *image = [UIImage imageNamed:@"Curry.png"];
//
// UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// imageView.frame = CGRectMake(100, 100, 100, 200);
// [self.view addSubview:imageView];
//
// [imageView release];
UIImage *image = [UIImage imageNamed:@"zuozuomuxi.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 160, 360, 360)];
imageView.image = image;
[self.view addSubview:imageView];
[imageView release];
// UIImageView相當(dāng)于相框,用來顯示,UIImage顯示的內(nèi)容
// 把圖片的交互打開
imageView.userInteractionEnabled = YES;
// 手勢(shì) Gesture
// 1.輕拍Tap
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 設(shè)置需要點(diǎn)擊幾次才會(huì)觸發(fā)方法
tap.numberOfTapsRequired = 2;
tap.numberOfTouchesRequired = 2;
// 把手勢(shì)加入到圖片上
[imageView addGestureRecognizer:tap];
// 內(nèi)存管理
[tap release];
// 2.長(zhǎng)按 LongPress
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
// 設(shè)置一下長(zhǎng)按需要的最短時(shí)間
longPress.minimumPressDuration = 3;
// 判斷在長(zhǎng)按過程中允許手指移動(dòng)的距離
longPress.allowableMovement = 100;
[imageView addGestureRecognizer:longPress];
[longPress release];
// 3.旋轉(zhuǎn) rotation
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imageView addGestureRecognizer:rotation];
[rotation release];
// 4.捏合 pinch
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch];
[pinch release];
// 5.拖拽 pan
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imageView addGestureRecognizer:pan];
[pan release];
// 6.輕掃
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
[imageView addGestureRecognizer:swipe];
[swipe release];
// 輕掃的方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
}
- (void)tapAction:(id)sender {
NSLog(@"taped an image");
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
// 長(zhǎng)按的方法在手勢(shì)的各個(gè)狀態(tài)中都會(huì)進(jìn)行觸發(fā),所以需要進(jìn)行判斷
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"開始長(zhǎng)按了");
}
NSLog(@"long pressed");
}
- (void)rotationAction:(UIRotationGestureRecognizer *)rotationGesture {
// 獲得添加手勢(shì)的視圖
UIImageView *imageView = (UIImageView *)[rotationGesture view];
// 調(diào)整視圖的transform屬性(順時(shí)針正數(shù),逆時(shí)針負(fù)數(shù))
imageView.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinchGesture {
UIImageView *imageView = (UIImageView *)[pinchGesture view];
imageView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);
// imageView.transform = CGAffineTransformScale(imageView.transform, pinchGesture.scale, pinchGesture.scale);
// pinchGesture.scale = 1;
}
- (void)panAction:(UIPanGestureRecognizer *)panGesture {
// 獲取拖拽手勢(shì)添加的視圖
UIImageView *imageView = (UIImageView *)[panGesture view];
// 獲取手勢(shì)經(jīng)過的點(diǎn)
CGPoint p = [panGesture translationInView:imageView];
// 然后對(duì)視圖的transform屬性進(jìn)行改變(橫移x變y不變 豎移x不變y變)
imageView.transform = CGAffineTransformMakeTranslation(p.x, p.y);
// imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);
// [panGesture setTranslation:CGPointZero inView:imageView];
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipeGesture {
if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"向左");
} else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右");
} else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"向上");
} else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"向下");
}
}
以上是關(guān)于手勢(shì)的一些使用,先記一下以防以后忘記。
另外需要注意: 一個(gè)手勢(shì)只能添加到一個(gè)view上 一個(gè)view可以添加多個(gè)手勢(shì) 估計(jì)和一個(gè)view只能有一個(gè)superview一樣 一個(gè)guesture也只能對(duì)應(yīng)一個(gè)view吧. 猜得.