国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS輕點(diǎn)、觸摸和手勢(shì)代碼開發(fā)

2020-07-26 03:08:52
字體:
供稿:網(wǎng)友

一、響應(yīng)者鏈

以UIResponder作為超類的任何類都是響應(yīng)者。UIView和UIControl是UIReponder的子類,因此所有視圖和所有控件都是響應(yīng)者。

1、初始相應(yīng)器
事件首先會(huì)傳遞給UIApplication對(duì)象,接下來會(huì)傳遞給應(yīng)用程序的UIWindow,UIWindow會(huì)選擇一個(gè)初始相應(yīng)器來處理事件。初始響應(yīng)器會(huì)選擇下面的方式選擇1、對(duì)于觸摸事件,UIWindow會(huì)確定用戶觸摸的視圖,然后將事件交給注冊(cè)了這個(gè)視圖的手勢(shì)識(shí)別器或則注冊(cè)視圖層級(jí)更高的手勢(shì)識(shí)別器。只要存在能處理事件的識(shí)別器,就不會(huì)再繼續(xù)找了。如果沒有的話,被觸摸視圖就是初始相應(yīng)器,事件也會(huì)傳遞給它。

2、對(duì)于用戶搖晃設(shè)備產(chǎn)生的或者是來自遠(yuǎn)程遙控設(shè)備事件,將會(huì)傳遞給第一響應(yīng)器
如果初始響應(yīng)器不處理時(shí)間,它會(huì)將事件傳遞給它的父視圖(如果存在的話),或者傳遞給視圖控制器(如果此視圖是視圖控制器的視圖)。如果視圖控制器不處理事件,它將沿著響應(yīng)器鏈的層級(jí)繼續(xù)傳給父視圖控制器(如果存在的話)。
如果在整個(gè)視圖層級(jí)中都沒與能處理事件的視圖或控制器,事件就會(huì)被傳遞給應(yīng)用程序的窗口。如果窗口不能處理事件,而應(yīng)用委托是UIResponder的子類,UIApplication對(duì)象就會(huì)將其傳遞給應(yīng)用程序委托。最后,如果應(yīng)用委托不是UIResponder的子類,或者不處理這個(gè)事件,那么這個(gè)事件將會(huì)被丟棄。

4個(gè)手勢(shì)通知方法

#pragma mark - Touch Event Methods// 用戶第一次觸摸屏幕時(shí)被調(diào)用- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{}// 當(dāng)發(fā)生某些事件(如來電呼叫)導(dǎo)致手勢(shì)中斷時(shí)被調(diào)用- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{}// 當(dāng)用戶手指離開屏幕時(shí)被調(diào)用- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{}// 當(dāng)用戶手指移動(dòng)時(shí)觸發(fā)- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{}

二、檢測(cè)掃描事件

1、手動(dòng)檢測(cè)

//// ViewController.m// Swipes//// Created by Jierism on 16/8/4.// Copyright © 2016年 Jierism. All rights reserved.//#import "ViewController.h"http:// 設(shè)置檢測(cè)范圍static CGFloat const kMinimmGestureLength = 25;static CGFloat const kMaximmVariance = 5;@interface ViewController ()@property (weak, nonatomic) IBOutlet UILabel *label;@property (nonatomic) CGPoint gestureStartPoint;@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning {  [super didReceiveMemoryWarning];  // Dispose of any resources that can be recreated.}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{  UITouch *touch = [touches anyObject];  self.gestureStartPoint = [touch locationInView:self.view];}- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{  UITouch *touch = [touches anyObject];  CGPoint currentPosition = [touch locationInView:self.view];  // 返回一個(gè)float的絕對(duì)值  CGFloat deltaX = fabsf(self.gestureStartPoint.x - currentPosition.x);  CGFloat deltaY = fabsf(self.gestureStartPoint.y - currentPosition.y);    // 獲得兩個(gè)增量后,判斷用戶在兩個(gè)方向上移動(dòng)過的距離,檢測(cè)用戶是否在一個(gè)方向上移動(dòng)得足夠遠(yuǎn)但在另一個(gè)方向移動(dòng)得不夠來形成輕掃動(dòng)作  if (deltaX >= kMinimmGestureLength && deltaY <= kMaximmVariance) {    self.label.text = @"Horizontal swipe detected";    // 2s后擦除文本    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),            dispatch_get_main_queue(),            ^{      self.label.text = @"";    });  }else if (deltaY >= kMinimmGestureLength && deltaX <= kMaximmVariance){    self.label.text = @"Vertical swipe detected";    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{      self.label.text = @"";    });  }}@end

2、識(shí)別器檢測(cè)

//// ViewController.m// Swipes//// Created by Jierism on 16/8/4.// Copyright © 2016年 Jierism. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UILabel *label;@property (nonatomic) CGPoint gestureStartPoint;@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib.  //創(chuàng)建兩個(gè)手勢(shì)識(shí)別器  // 1、水平方向識(shí)別器  UISwipeGestureRecognizer *horizontal = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(reportHorizontalSwipe:)];  horizontal.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;  [self.view addGestureRecognizer:horizontal];    // 2、垂直方向識(shí)別器  UISwipeGestureRecognizer *vertical = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(reportVerticalSwipe:)];  vertical.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;  [self.view addGestureRecognizer:vertical];}- (void)didReceiveMemoryWarning {  [super didReceiveMemoryWarning];  // Dispose of any resources that can be recreated.}- (void)reportHorizontalSwipe:(UIGestureRecognizer *)recognizer{  self.label.text = @"Horizontal swipe detected";  // 2s后擦除文本  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),          dispatch_get_main_queue(),          ^{            self.label.text = @"";          });}- (void)reportVerticalSwipe:(UIGestureRecognizer *)recognizer{  self.label.text = @"Vertical swipe detected";  // 2s后擦除文本  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),          dispatch_get_main_queue(),          ^{            self.label.text = @"";          });}@end

三、實(shí)現(xiàn)多指輕掃

//// ViewController.m// Swipes//// Created by Jierism on 16/8/4.// Copyright © 2016年 Jierism. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UILabel *label;@property (nonatomic) CGPoint gestureStartPoint;@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib.    for (NSUInteger touchCount = 1; touchCount <= 5; touchCount++) {    //創(chuàng)建兩個(gè)手勢(shì)識(shí)別器    // 1、水平方向識(shí)別器    UISwipeGestureRecognizer *horizontal = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(reportHorizontalSwipe:)];        horizontal.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;    [self.view addGestureRecognizer:horizontal];        // 2、垂直方向識(shí)別器    UISwipeGestureRecognizer *vertical = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(reportVerticalSwipe:)];    vertical.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;    [self.view addGestureRecognizer:vertical];  }  }- (void)didReceiveMemoryWarning {  [super didReceiveMemoryWarning];  // Dispose of any resources that can be recreated.}- (NSString *)descriptionForTouchCount:(NSUInteger)touchCount{  switch (touchCount) {    case 1:      return @"Single";    case 2:      return @"Double";    case 3:      return @"Triple";    case 4:      return @"Quadruple";    case 5:      return @"Quintuple";          default:      return @"";  }}- (void)reportHorizontalSwipe:(UIGestureRecognizer *)recognizer{  self.label.text = [NSString stringWithFormat:@"%@ Horizontal swipe detected",[self descriptionForTouchCount:[recognizer numberOfTouches]]];  // 2s后擦除文本  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),          dispatch_get_main_queue(),          ^{            self.label.text = @"";          });}- (void)reportVerticalSwipe:(UIGestureRecognizer *)recognizer{  self.label.text = [NSString stringWithFormat:@"%@ Vertical swipe detected",[self descriptionForTouchCount:[recognizer numberOfTouches]]];  // 2s后擦除文本  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),          dispatch_get_main_queue(),          ^{            self.label.text = @"";          });}@end

四、檢測(cè)多次輕點(diǎn)

//// ViewController.m// TapTaps//// Created by Jierism on 16/8/4.// Copyright © 2016年 Jierism. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UILabel *singleLabel;@property (weak, nonatomic) IBOutlet UILabel *doubleLabel;@property (weak, nonatomic) IBOutlet UILabel *tripleLabel;@property (weak, nonatomic) IBOutlet UILabel *quadrupleLabel;@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib.  // 創(chuàng)建4個(gè)點(diǎn)擊手勢(shì)識(shí)別器  UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];  singleTap.numberOfTapsRequired = 1;  singleTap.numberOfTouchesRequired = 1;  // 附加到視圖  [self.view addGestureRecognizer:singleTap];    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap)];  doubleTap.numberOfTapsRequired = 2;  doubleTap.numberOfTouchesRequired = 1;  [self.view addGestureRecognizer:doubleTap];  // 當(dāng)doubleTap響應(yīng)“失敗”才運(yùn)行singleTap  [singleTap requireGestureRecognizerToFail:doubleTap];    UITapGestureRecognizer *tripleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tripleTap)];  tripleTap.numberOfTapsRequired = 3;  tripleTap.numberOfTouchesRequired = 1;  [self.view addGestureRecognizer:tripleTap];  [doubleTap requireGestureRecognizerToFail:tripleTap];    UITapGestureRecognizer *quadrupleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(quadrupleTap)];  quadrupleTap.numberOfTapsRequired = 4;  quadrupleTap.numberOfTouchesRequired = 1;  [self.view addGestureRecognizer:quadrupleTap];  [tripleTap requireGestureRecognizerToFail:quadrupleTap];}- (void)singleTap{  self.singleLabel.text = @"Single Tap Detected";  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{    self.singleLabel.text = @"";  });}- (void)doubleTap{  self.doubleLabel.text = @"Double Tap Detected";  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{    self.doubleLabel.text = @"";  });}- (void)tripleTap{  self.tripleLabel.text = @"Triple Tap Detected";  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{    self.tripleLabel.text = @"";  });}- (void)quadrupleTap{  self.quadrupleLabel.text = @"Quadruple Tap Detected";  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{    self.quadrupleLabel.text = @"";  });}@end

五、檢測(cè)捏合和旋轉(zhuǎn)

#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UIGestureRecognizerDelegate>@end

//// ViewController.m// PinchMe//// Created by Jierism on 16/8/4.// Copyright © 2016年 Jierism. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (strong,nonatomic) UIImageView *imageView;@end@implementation ViewController// 當(dāng)前縮放比例,先前縮放比例CGFloat scale,previousScale;// 當(dāng)前旋轉(zhuǎn)角度,先前旋轉(zhuǎn)角度CGFloat rotation,previousRotation;- (void)viewDidLoad {  [super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib.  previousScale = 1;    UIImage *image = [UIImage imageNamed:@"yosemite-meadows"];  self.imageView = [[UIImageView alloc] initWithImage:image];  // 對(duì)圖像啟用交互功能  self.imageView.userInteractionEnabled = YES;  self.imageView.center = self.view.center;  [self.view addSubview:self.imageView];    // 建立捏合手勢(shì)識(shí)別器  UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(doPinch:)];  pinchGesture.delegate = self;  [self.imageView addGestureRecognizer:pinchGesture];    // 建立旋轉(zhuǎn)手勢(shì)識(shí)別器  UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(doRorate:)];  rotationGesture.delegate = self;  [self.imageView addGestureRecognizer:rotationGesture];}- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{  // 允許捏合手勢(shì)和旋轉(zhuǎn)手勢(shì)同時(shí)工作。否則,先開始的手勢(shì)識(shí)別器會(huì)屏蔽另一個(gè)  return YES;}// 根據(jù)手勢(shì)識(shí)別器中獲得的縮放比例和旋轉(zhuǎn)角度對(duì)圖像進(jìn)行變換- (void)transformImageView{  CGAffineTransform t = CGAffineTransformMakeScale(scale * previousScale, scale * previousScale);  t = CGAffineTransformRotate(t, rotation + previousRotation);  self.imageView.transform = t;}- (void)doPinch:(UIPinchGestureRecognizer *)gesture{  scale = gesture.scale;  [self transformImageView];  if (gesture.state == UIGestureRecognizerStateEnded) {    previousScale = scale * previousScale;    scale = 1;  }}- (void)doRorate:(UIRotationGestureRecognizer *)gesture{  rotation = gesture.rotation;  [self transformImageView];  if (gesture.state == UIGestureRecognizerStateEnded) {    previousRotation = rotation + previousRotation;    rotation = 0;  }}@end

六、自定義手勢(shì)

//// CheckMarkRecognizer.m// CheckPlease//// Created by Jierism on 16/8/4.// Copyright © 2016年 Jierism. All rights reserved.//#import "CheckMarkRecognizer.h"#import "CGPointUtils.h"#import <UIKit/UIGestureRecognizerSubclass.h> // 一個(gè)重要目的是使手勢(shì)識(shí)別器的state屬性可寫,子類將使用這個(gè)機(jī)制斷言我們所觀察的手勢(shì)已成功完成// 設(shè)置檢測(cè)范圍static CGFloat const kMinimunCheckMarkAngle = 80;static CGFloat const kMaximumCheckMarkAngle = 100;static CGFloat const kMinimumCheckMarkLength = 10;@implementation CheckMarkRecognizer{  // 前兩個(gè)實(shí)例變量提供之前的線段  CGPoint lastPreviousPoint;  CGPoint lastCurrentPoint;  // 畫出的線段長(zhǎng)度  CGFloat lineLengthSoFar;}// 用lastPreviousPoint和lastCurrentPoint組成第一條線段,跟第二條線段形成角度去完成手勢(shì)- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{  [super touchesBegan:touches withEvent:event];  UITouch *touch = [touches anyObject];  CGPoint point = [touch locationInView:self.view];  lastPreviousPoint = point;  lastCurrentPoint = point;  lineLengthSoFar = 0.0;}- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{  [super touchesMoved:touches withEvent:event];  UITouch *touch = [touches anyObject];  CGPoint previousPoint = [touch previousLocationInView:self.view];  CGPoint currentPoint = [touch locationInView:self.view];  CGFloat angle = angleBetweenLines(lastPreviousPoint, lastCurrentPoint, previousPoint, currentPoint);  if (angle >= kMinimunCheckMarkAngle && angle <= kMaximumCheckMarkAngle && lineLengthSoFar > kMinimumCheckMarkLength) {    self.state = UIGestureRecognizerStateRecognized;  }  lineLengthSoFar += distanceBetweenPoints(previousPoint, currentPoint);  lastPreviousPoint = previousPoint;  lastCurrentPoint = currentPoint;}@end
//// ViewController.m// CheckPlease//// Created by Jierism on 16/8/4.// Copyright © 2016年 Jierism. All rights reserved.//#import "ViewController.h"#import "CheckMarkRecognizer.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib.  CheckMarkRecognizer *check = [[CheckMarkRecognizer alloc] initWithTarget:self action:@selector(doCheck:)];  [self.view addGestureRecognizer:check];  self.imageView.hidden = YES;}- (void)didReceiveMemoryWarning {  [super didReceiveMemoryWarning];  // Dispose of any resources that can be recreated.}- (void)doCheck:(CheckMarkRecognizer *)check{  self.imageView.hidden = NO;  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{    self.imageView.hidden = YES;  });}@end

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁津县| 苍溪县| 巨野县| 秦安县| 朝阳市| 松江区| 河间市| 旬阳县| 漠河县| 陕西省| 醴陵市| 凤冈县| 台湾省| 开原市| 新竹市| 扎囊县| 鹤山市| 安国市| 中江县| 康平县| 香河县| 文昌市| 绩溪县| 岳普湖县| 太湖县| 榆中县| 泾川县| 广河县| 商都县| 正阳县| 西畴县| 兴隆县| 厦门市| 吉水县| 左贡县| 宁明县| 安泽县| 拉萨市| 通河县| 东方市| 邻水|