iOS開發之UI手勢

基礎手勢

@property (nonatomic,strong)UIImageView *imageView;//該方法是一個懶加載,在需要的地方才調用,節省內存- (UIImageView *)imageView{//方法名要與屬性相同
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 70, self.view.bounds.size.width-40, self.view.bounds.size.height/2)];
        _imageView.image = [UIImage imageNamed:@"1.jpg"];
    }    return _imageView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.imageView];//此處相當于調用了懶加載方法

    //開啟交互事件的響應,默認為NO
    _imageView.userInteractionEnabled = YES;    //添加手勢對象
    /**
     UITapGestureRecognizer 點按手勢

    參數1:響應事件擁有者,self表示當前視圖控制器
    參數2:響應事件方法,tapOneAction:
     */
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOneAction:)];    //表示識別事件的類型:幾次點擊觸發,默認值是1
    tapGesture.numberOfTapsRequired = 1;    //幾個手指點擊時觸發事件
    tapGesture.numberOfTouchesRequired = 1;    //把手勢添加到視圖里面,視圖可以響應事件
    [_imageView addGestureRecognizer:tapGesture];    //添加雙擊手勢
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwoAction:)];
    tapGesture2.numberOfTapsRequired = 2;//點擊兩次才觸發事件
    tapGesture2.numberOfTouchesRequired = 1;
    [_imageView addGestureRecognizer:tapGesture2];    //當單擊操作遇到雙擊操作的時候,使單擊操作失敗(tapGesture)
    [tapGesture requireGestureRecognizerToFail:tapGesture2];  
}//單擊事件響應方法,這里使圖片放大,即改變imageView的frame- (void)tapOneAction:(UITapGestureRecognizer *)gesture{    NSLog(@"單擊");    //獲取手勢監控的視圖,在該imageView外面不能觸發
    UIImageView *imageView = (UIImageView *)gesture.view;    //開始動畫
    [UIView beginAnimations:nil context:nil];    //設置動畫時間
    [UIView setAnimationDuration:2];    //放大圖片
    imageView.frame = self.view.bounds; 
}//雙擊事件響應方法,這里使圖片縮小為原來的大小- (void)tapTwoAction:(UITapGestureRecognizer *)gesture{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2];    //設置_imageView為初始的frame
    _imageView.frame = CGRectMake(20, 70, self.view.bounds.size.width-40, self.view.bounds.size.height/2);    //提交動畫
    [UIView commitAnimations]; 
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465

高級手勢

#import "ViewController.h"@interface ViewController ()<UIGestureRecognizerDelegate>//UIGestureRecognizerDelegate 同時進行旋轉、縮放的協議@property (nonatomic,strong)UIImageView *imageView;//定義一個縮放手勢手勢,用來對視圖進行放大縮小//捏合手勢@property (nonatomic,strong)UIPinchGestureRecognizer *pinGesture;//旋轉手勢@property (nonatomic,strong)UIRotationGestureRecognizer *rotationGesture;@[email protected] ViewController//懶加載- (UIImageView *)imageView {    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 70, self.view.bounds.size.width-40, self.view.bounds.size.height/2)];
        _imageView.image = [UIImage imageNamed:@"2.jpg"];

    }    return _imageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.imageView];    //開啟交互
    _imageView.userInteractionEnabled = YES;    //創建一個捏合手勢
    _pinGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinGesture:)];
    [_imageView addGestureRecognizer:_pinGesture];    //創建一個旋轉手勢
    _rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [_imageView addGestureRecognizer:_rotationGesture];    //設置兩種手勢的代理為self
    _pinGesture.delegate = self;
    _rotationGesture.delegate = self;
}//實現協議的方法//是否可以同時響應兩個手勢- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {//Simultaneously 同時的
    //如果返回YES,可以同時響應
    return YES;
}

- (void)pinGesture:(UIPinchGestureRecognizer *)gesture {    //獲取監控圖像視圖
    UIImageView *imgView = (UIImageView *)gesture.view;    //對圖像視圖對象進行矩陣計算并且賦值
    //transform:表示圖像學中的變換矩陣
    //CGAffineTransformScale:通過縮放產生一個新的矩陣(在原來的基礎上)
    //p1:原來的矩陣
    //p2:x方向縮放的比例
    //p3:y方向縮放的比例
    imgView.transform = CGAffineTransformScale(imgView.transform, gesture.scale, gesture.scale);    //返回的是新的縮放之后變化矩陣的比例
    gesture.scale = 1;
}

- (void)rotationAction:(UIRotationGestureRecognizer *)rotationGesture {    //獲取監控圖像視圖
    UIImageView *imgView = (UIImageView *)rotationGesture.view;
    imgView.transform = CGAffineTransformRotate(imgView.transform, rotationGesture.rotation);    //對角度進行清零
    rotationGesture.rotation = 0;
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273

擴展手勢

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>@property (nonatomic,strong)UIImageView *imageView;@[email protected] ViewController- (UIImageView *)imageView {    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 70, self.view.bounds.size.width-40, self.view.bounds.size.height/2)];
        _imageView.image = [UIImage imageNamed:@"2.jpg"];

    }    return _imageView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.imageView];
    _imageView.userInteractionEnabled = YES;    //創建一個平移手勢
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];    //添加手勢
    [_imageView addGestureRecognizer:panGesture];    //創建一個輕掃手勢
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
    [_imageView addGestureRecognizer:swipeGesture];    //創建一個長按手勢
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];    //設置長按手勢的時間,默認0.5秒為長按手勢
    longGesture.minimumPressDuration = 1.0;
    [_imageView addGestureRecognizer:longGesture];
}//平移手勢//只要手指在屏幕坐標上發生變化的時候就會調用該方法- (void)panAction:(UIPanGestureRecognizer *)panGesture {//    NSLog(@"平移手勢");//    //獲得移動的坐標,相對于視圖的坐標系
    CGPoint point = [panGesture translationInView:self.view];//translation 平移
    _imageView.center = CGPointMake(_imageView.center.x + point.x, _imageView.center.y + point.y);    //在指定視圖的坐標系中設置平移值
    [panGesture setTranslation:CGPointZero inView:self.view];//    NSLog(@"point.x = %f,point.y = %f",point.x,point.y);//    //獲取平移時的相對速度(每秒在屏幕上滑動移動像素的值)
    CGPoint pv = [panGesture velocityInView:self.view];//    NSLog(@"pv.x = %.2f,pv.y = %.2f",pv.x,pv.y);}//輕掃手勢- (void)swipeAction:(UISwipeGestureRecognizer *)swipeGesture {    NSLog(@"向上");
}//長按手勢- (void)longAction:(UILongPressGestureRecognizer *)longGesture {    if (longGesture.state == UIGestureRecognizerStateBegan) {        NSLog(@"開始狀態");

        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"保存到相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {            NSLog(@"保存到相冊");
            UIImageWriteToSavedPhotosAlbum(_imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }];
        [alertC addAction:action1];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"打開相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {            NSLog(@"打開相冊");            /**
             sourceType:
             UIImagePickerControllerSourceTypePhotoLibrary,    圖片列表
             UIImagePickerControllerSourceTypeCamera,          攝像頭
             UIImagePickerControllerSourceTypeSavedPhotosAlbum 相冊
             */
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];            //資源類型
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            picker.delegate = self;            //選擇圖片后是否可以被編輯
            picker.allowsEditing = YES;            //推送picker控制器
            [self presentViewController:picker animated:YES completion:nil];

        }];
        [alertC addAction:action2];
        UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"打開相機" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {            NSLog(@"打開相機");            //要使用真機來測試
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.sourceType = UIBarButtonSystemItemCamera;
            picker.delegate = self;
            picker.allowsEditing = YES;
            [self presentViewController:picker animated:YES completion:nil];


        }];
        [alertC addAction:action3];
        UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {            NSLog(@"取消");
        }];
        [alertC addAction:action4];        //推出alertC??!
        [self presentViewController:alertC animated:YES completion:^{

        }];

    }    else if (longGesture.state == UIGestureRecognizerStateEnded){        NSLog(@"結束狀態");
    }    NSLog(@"長按");
}//保存圖片方法- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{    if (!error) {        NSLog(@"圖片保存成功");
    }    else {        NSLog(@"圖片保存失敗");
    }
}#pragma mark --UIImagePickerControllerDelegate--- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {        NSLog(@"%@",info);        //照片的原始數據
        _imageView.image = info[UIImagePickerControllerOriginalImage];        //退出picker?。?!
    }    else if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        _imageView.image = info[UIImagePickerControllerOriginalImage];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {//    //    NSLog(@"取消");//}@end

來源:CSDN

上一篇: iOS系統3DTouch全解析

下一篇: ui設計和美工你可以這樣理解

分享到: 更多
终于发现分分快3稳赚公式 秒速时时开挂软件 牛牛娱乐棋牌 时时彩四星稳赚方法 bet 365英国 足球体育 腾讯分分彩包胆技巧 28杠生死门8个口诀 二人麻将怎么玩 老重庆时时彩开奖网站 时时彩综合走势图重庆 重庆时时采彩开奖今天 重庆时时开彩结果 北京pk拾最新公式规律 本金少的玩彩稳赚法 pk104码倍投方法 北京pk106码技巧规律