1. 程式人生 > >iOS 手勢的使用 六個手勢 輕拍 長按 拖拽 捏合 輕掃 旋轉

iOS 手勢的使用 六個手勢 輕拍 長按 拖拽 捏合 輕掃 旋轉


上面的手勢對應的操作是:
Tap(點一下) Pinch(二指往內或往外撥動,平時經常用到的縮放) Rotation(旋轉) Swipe(滑動,快速移動) Pan (拖移,慢速移動) LongPress(長按)
2、使用手勢的步驟

使用手勢很簡單,分為兩步:
建立手勢例項。當建立手勢時,指定一個回撥方法,當手勢開始,改變、或結束時,回撥方法被呼叫。 新增到需要識別的View中。每個手勢只對應一個View,當螢幕觸控在View的邊界內時,如果手勢和預定的一樣,那就會回撥方法。

ps:一個手勢只能對應一個View,但是一個View可以有多個手勢。

手勢具體操作 如下:

新建一個mainViewController 繼承於 UIViewcontroller ,

mainViewController.m

#import "mainViewController.h"

@interface mainViewController ()

@property (retain, nonatomic)UIImageView *imageView;

@end

@implementation mainViewController

- (void)dealloc{

    [self.imageView release];

    [super dealloc];

}

- (instancetype)initWithNibName:(NSString

*)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

    }

return self;

}

- (void)viewDidLoad {

    [superviewDidLoad];

// Do any additional setup after loading the view.

    [selfcreateSubViews];

}

- (void

)createSubViews{

self.imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(70, 130, 220, 350)];

    [_imageViewsetImage:[UIImageimageNamed:@"1.jpg"]];

_imageView.userInteractionEnabled = YES;

    [self.view addSubview:_imageView];

    [_imageView release];

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"點選", @"長按", @"輕掃", @"捏合", @"旋轉", @"拖拽", nil]];

    segmentedControl.frame = CGRectMake(30, 600, 315, 50);

    segmentedControl.momentary = YES;

    segmentedControl.tintColor = [UIColor blackColor];

    [segmentedControl addTarget:selfaction:@selector(action:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:segmentedControl];

    [segmentedControl release]; 

}

#pragma mari - UISegmentedControl的點選事件

- (void)action:(id)sender{

// 分段點選按鈕

UISegmentedControl *seg = (UISegmentedControl *)sender;

// 移除imageView的所有手勢,重新新增新的手勢

// 第一步:獲得一個檢視的所有手勢

NSArray *gestures = self.imageView.gestureRecognizers;

// 第二步:移除

    for (UIGestureRecognizer *ges in gestures) {

        [self.imageViewremoveGestureRecognizer:ges];

    }

switch (seg.selectedSegmentIndex) {

        case 0:

        {

            // 建立一個點選手勢

UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tapAction:)];

            [self.imageView addGestureRecognizer:tap];

            [tap release];

        }

            break;

        case 1:

        {

            // 長按手勢

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPressAction:)];

            [self.imageView addGestureRecognizer:longPress];

            [longPress release];

        }

            break;

        case 2:

        {

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipeAction:)];

// 一個輕掃手勢只能有一個輕掃方向

            // 設定輕掃的方向

            swipe.direction = UISwipeGestureRecognizerDirectionDown;

            [self.imageView addGestureRecognizer:swipe];

            [swipe release];

        }

            break;

        case 3:

        {

            // 捏合手勢

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc] initWithTarget:selfaction:@selector(pinchAciton:)];

            [self.imageView addGestureRecognizer:pinch];

            [pinch release];

        }

            break;

        case 4:

        {

            // 旋轉手勢

UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizeralloc] initWithTarget:selfaction:@selector(rotateAction:)];

            [self.imageView addGestureRecognizer:rotate];

            [rotate release];

        }

            break;

        case 5:

        {

            // 拖拽手勢

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(panAction:)];

            [self.imageView addGestureRecognizer:pan];

            [pan release];

        }

            break;

        default:

            break;

    }

}

#pragma mark - 內部的點選手勢事件

#pragma mark 點選

- (void)tapAction:(UITapGestureRecognizer *)tap{

// 通過手勢獲得手勢所在的檢視

//    UIImageView *aImage = (UIImageView *)tap.view;

// 通過不同的狀態做不同的事

if (tap.state == UIGestureRecognizerStateBegan) {

        NSLog(@"開始點選");

    }

    NSLog(@"%s", __func__);

}

#pragma mark 長按

- (void)longPressAction:(UILongPressGestureRecognizer *)press{

if (press.state == UIGestureRecognizerStateBegan) {

        NSLog(@"begin");

    }

}

#pragma mark 輕掃

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{

        NSLog(@"橫掃千軍");

}

#pragma mark 捏合

- (void)pinchAciton:(UIPinchGestureRecognizer *)pinch{

    NSLog(@"捏合");

    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);

    pinch.scale = 1.0f;

}

#pragma mark 旋轉

- (void)rotateAction:(UIRotationGestureRecognizer *)ges{

// 旋轉的基礎角度為0, 每次旋轉的角度都是以當前中軸線為軸來判定的

self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, ges.rotation);

    ges.rotation = 0;

NSLog(@"Rotation = %f", ges.rotation);

}

#pragma mark 拖拽

- (void)panAction:(UIPanGestureRecognizer *)pan{

    CGPoint point = [pan translationInView:self.imageView];

// 偏移量會在每次偏移之後,在偏移後的基礎上增加

    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);

// 重新把偏移量歸零

    [pan setTranslation:CGPointZeroinView:self.imageView];

}


AppDelegate.m

#import "AppDelegate.h"

#import "mainViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc{

    [super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

_window.backgroundColor = [UIColorwhiteColor];

    [_windowmakeKeyAndVisible];

    [_window release];

mainViewController *myView = [[mainViewControlleralloc] init];

    [self.windowsetRootViewController:myView];

    [myView release];

return YES;

}