最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS手勢的實現(xiàn)方法

 更新時間:2017年03月13日 09:45:58   作者:makingitbest  
這篇文章主要為大家詳細(xì)介紹了iOS手勢的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了iOS手勢的具體實現(xiàn)代碼,供大家參考,具體內(nèi)容如下

效果

細(xì)節(jié)

1.UITouch

#import "ViewController_0.h"

@interface ViewController_0 ()

@property (nonatomic, strong)UILabel *label;

@end

@implementation ViewController_0

- (void)viewDidLoad {
  
  [super viewDidLoad];
  
  self.label          = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
  self.label.backgroundColor  = [UIColor yellowColor];
  self.label.layer.borderWidth = 1;
  [self.view addSubview:self.label];
  
  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"拖動方塊";
  [textlabel sizeToFit];
  textlabel.font   = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

  //1.拿到手勢
  UITouch *touch = [touches anyObject];
  
  //2.拿到touch 所在view的坐標(biāo)
  CGPoint point = [touch locationInView:self.view];
  
  //3.讓label拿到坐標(biāo)
  self.label.center = point;
  
  NSLog(@"1.手指接觸到了屏幕");
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

  //1.拿到手勢
  UITouch *touch = [touches anyObject];
  
  //2.拿到touch 所在view的坐標(biāo)
  CGPoint point = [touch locationInView:self.view];
  
  //3.讓label拿到坐標(biāo)
  self.label.center = point;
  
  NSLog(@"2.手指在屏幕上移動");
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

   NSLog(@"3.手指剛離開屏幕");
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

  NSLog(@"4.手勢失效了");
}

@end

2.UITapGestureRecognizer

#import "ViewController_1.h"

@interface ViewController_1 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_1

- (void)viewDidLoad {
  
  [super viewDidLoad];

  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"電腦上操作tap手勢 alt +shift 需要連續(xù)點擊次數(shù)2次";
  [textlabel sizeToFit];
  textlabel.font   = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];
  
  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(100, 150, 100, 100)];
  self.label.backgroundColor    = [UIColor orangeColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];
  
  // 1.創(chuàng)建tap手勢
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];
  tap.numberOfTapsRequired  = 2; //需要輕輕點擊的次數(shù)
  tap.numberOfTouchesRequired = 2;//需要的手指數(shù)量 :2根手指alt +shift 需要匹配點擊次數(shù)2次(其實直接用默認(rèn)的就好)
  [self.label addGestureRecognizer:tap];
}

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

  int num = [self.label.text intValue];
  num++;
  self.label.text = [NSString stringWithFormat:@"%d",num ];
}

@end

3.UILongPressGestureRecognizer

#import "ViewController_2.h"

@interface ViewController_2 () <UIGestureRecognizerDelegate>

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_2

- (void)viewDidLoad {
  
  [super viewDidLoad];
  
  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"long長按手勢";
  [textlabel sizeToFit];
  textlabel.font   = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];
  
  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(40, 150, 200, 150)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];
  
  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
  longPress.numberOfTapsRequired     = 1;
  longPress.numberOfTouchesRequired    = 1;
  longPress.minimumPressDuration     = 1.0;
  longPress.delegate  = self;
  [self.label addGestureRecognizer:longPress];
}

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

  if(longPress.state == UIGestureRecognizerStateBegan) {
    
    NSLog(@"手勢狀態(tài)開始");
    
  } else if(longPress.state == UIGestureRecognizerStateEnded) {
    
    NSLog(@"手勢狀態(tài)結(jié)束");
  
  } else if(longPress.state == UIGestureRecognizerStateChanged) {
    
    NSLog(@"手勢狀態(tài)改變");
    
    NSInteger num = [self.label.text integerValue];
    num ++;
    self.label.text = [NSString stringWithFormat:@"%ld",(long)num];
  }
}

@end 

4.UISwipeGestureRecognizer

#import "ViewController_3.h"

@interface ViewController_3 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_3

- (void)viewDidLoad {
  
  [super viewDidLoad];
 
  UILabel *textlabel   = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text     = @"swipe手勢 向右滑動➕1,你也可以設(shè)置左劃上劃下劃";
  textlabel.numberOfLines = 0;
  textlabel.font     = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];
  
  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(100, 150, 100, 100)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];

  UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
  swipeGesture.direction         = UISwipeGestureRecognizerDirectionRight;//默認(rèn)就是向右劃
  [self.label addGestureRecognizer:swipeGesture];
}

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

  if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
    
    NSLog(@"現(xiàn)在響應(yīng)左劃手勢");
    
  } else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
    
    NSLog(@"現(xiàn)在響應(yīng)右劃手勢");
    
    NSInteger num  = [self.label.text integerValue];
    num ++;
    self.label.text = [NSString stringWithFormat:@"%ld",(long)num];
    
  } else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
    
    NSLog(@"現(xiàn)在響應(yīng)上劃手勢");
    
  } else if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
    
    NSLog(@"現(xiàn)在響應(yīng)下劃手勢");
  }
}

@end

 5.UIPanGestureRecognizer

#import "ViewController_4.h"

@interface ViewController_4 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_4

- (void)viewDidLoad {
  
  [super viewDidLoad];
  
  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"pan手勢,拖動方塊";
  [textlabel sizeToFit];
  textlabel.font   = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];

  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];

  UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
  [self.label addGestureRecognizer:pan];
}

- (void)panAction:(UIPanGestureRecognizer *)pan {
  
  CGPoint offset  = [pan locationInView:self.view];
  self.label.center = offset;
}

@end

6.UIRotationGestureRecognizer

#import "ViewController_5.h"

@interface ViewController_5 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_5

- (void)viewDidLoad {
  
  [super viewDidLoad];
  
  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"模擬器測試旋轉(zhuǎn)手勢時,按住 option鍵,再用觸摸板或鼠標(biāo)操作";
  textlabel.font   = [UIFont systemFontOfSize:12];
  textlabel.numberOfLines = 0;
  [self.view addSubview:textlabel];

  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(40, 200, 200, 50)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"we are friends";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];
  
  //(模擬器測試捏合和旋轉(zhuǎn)手勢時,按住 option 鍵,再用觸摸板或鼠標(biāo)操作)
  UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
  [self.view addGestureRecognizer:rotation];

}

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation {
  
  self.label.transform = CGAffineTransformRotate(self.label.transform, rotation.rotation);
  rotation.rotation = 0; // 這個很重要!!!!!

//  static float orginState;
//  
//  self.label.transform = CGAffineTransformMakeRotation(rotation.rotation + orginState);
//  
//  if (rotation.state == UIGestureRecognizerStateEnded) {
//    
//    orginState = orginState + rotation.state;
//    self.label.transform = CGAffineTransformMakeRotation(rotation.rotation);
//  }
}

@end

7.UIPinchGestureRecognizer

#import "ViewController_6.h"

@interface ViewController_6 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_6

- (void)viewDidLoad {
  
  [super viewDidLoad];
 
  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"模擬器測試捏合手勢時,按住 option鍵,再用觸摸板或鼠標(biāo)操作";
  textlabel.font   = [UIFont systemFontOfSize:12];
  textlabel.numberOfLines = 0;
  [self.view addSubview:textlabel];
  
  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 80, 80)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];
  
//  (模擬器測試捏合和旋轉(zhuǎn)手勢時,按住 option 鍵,再用觸摸板或鼠標(biāo)操作)
  UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pichGesture:)];
  [self.view addGestureRecognizer:pinch];
}

- (void)pichGesture:(UIPinchGestureRecognizer *)pinch {
  
  static float originScale = 1;
  
  //手勢縮放返回的scale 是相對于上一次的
  self.label.transform = CGAffineTransformMakeScale(pinch.scale * originScale , pinch.scale*originScale);
  
  if (pinch.state == UIGestureRecognizerStateEnded) {
    
    originScale = originScale * pinch.scale;
  }
}

@end

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解iOS按鈕暴力點擊的便捷解決方案

    詳解iOS按鈕暴力點擊的便捷解決方案

    本篇文章主要介紹了iOS按鈕暴力點擊的便捷解決方案,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2016-12-12
  • iOS Xcode8更新后輸出log日志關(guān)閉的方法

    iOS Xcode8更新后輸出log日志關(guān)閉的方法

    今天剛把xcode更新到了xcode8,運行發(fā)現(xiàn)好多l(xiāng)og輸出,怎么關(guān)閉呢,不是很清楚,通過查閱相關(guān)資料順利關(guān)掉這些log日志,下面小編把方法共享下,需要的朋友參考下
    2016-09-09
  • iOS中謂詞(NSPredicate)的基本入門使用教程

    iOS中謂詞(NSPredicate)的基本入門使用教程

    這篇文章主要給大家介紹了關(guān)于iOS中謂詞(NSPredicate)的基本入門使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • iOS繪制3D餅圖的實現(xiàn)方法

    iOS繪制3D餅圖的實現(xiàn)方法

    餅圖常用于統(tǒng)計學(xué)模塊。常見的一般為2D餅圖,這篇文章主要介紹了iOS繪制3D餅圖的實現(xiàn)方法,3D餅圖更加立體,用戶的好感度也比較高,下面需要的朋友可以參考借鑒,一起來看看吧。
    2017-01-01
  • iOS10實現(xiàn)推送功能時的注意點和問題總結(jié)

    iOS10實現(xiàn)推送功能時的注意點和問題總結(jié)

    很多朋友都反饋,發(fā)現(xiàn)了iOS9升級到iOS10推送功能不正常的問題,所以這篇文章總結(jié)了一下要點,親們可以根據(jù)以下步驟,逐步排查問題,也可以逐步實現(xiàn)iOS10的推送功能。下面來一起看看吧。
    2016-09-09
  • iOS 懶加載的使用實例代碼

    iOS 懶加載的使用實例代碼

    本篇文章主要介紹了iOS 懶加載的使用實例代碼,詳細(xì)的介紹了什么是懶加載和優(yōu)點,及其實例。有興趣的可以了解一下
    2017-05-05
  • IOS 基本文件操作實例詳解

    IOS 基本文件操作實例詳解

    這篇文章主要介紹了IOS 基本文件操作實例詳解 的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • iOS 底部按鈕和應(yīng)用圖標(biāo)顯示未讀消息(帶數(shù)字)

    iOS 底部按鈕和應(yīng)用圖標(biāo)顯示未讀消息(帶數(shù)字)

    本文主要介紹了iOS 底部按鈕和應(yīng)用圖標(biāo)顯示未讀消息的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • 關(guān)于iOS導(dǎo)航欄返回按鈕問題的解決方法

    關(guān)于iOS導(dǎo)航欄返回按鈕問題的解決方法

    這篇文章主要為大家詳細(xì)介紹了關(guān)于iOS導(dǎo)航欄返回按鈕問題的解決方法,對iOS自定義backBarButtonItem的點擊事件進(jìn)行介紹,感興趣的小伙伴們可以參考一下
    2016-05-05
  • iOS實現(xiàn)按鈕點擊選中與被選中切換功能

    iOS實現(xiàn)按鈕點擊選中與被選中切換功能

    這篇文章主要介紹了iOS實現(xiàn)按鈕點擊選中與被選中切換功能,需要的朋友可以參考下
    2017-07-07

最新評論

吉安市| 普格县| 平阴县| 平舆县| 临沧市| 南阳市| 乌审旗| 平凉市| 合山市| 安泽县| 商洛市| 开江县| 镇巴县| 彭州市| 凯里市| 且末县| 枣阳市| 城口县| 青铜峡市| 湖南省| 陇南市| 吉林省| 调兵山市| 林芝县| 南江县| 阿勒泰市| 鄂托克旗| 沈阳市| 安阳市| 乌拉特中旗| 东宁县| 新营市| 金阳县| 通辽市| 萝北县| 靖州| 五莲县| 阿拉善左旗| 乌鲁木齐市| 岱山县| 沈丘县|