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

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

IOS  手勢操作詳解及實例總結(jié)篇

2020-07-26 02:59:18
字體:
供稿:網(wǎng)友

iOS手勢操作總結(jié)

手勢操作種類

  • UITapGestureRecognizer: 敲擊,點擊
  • UILongPressGestureRecognizer: 長按
  • UIPinchGestureRecognizer: 縮放
  • UIRotationGestureRecognizer: 旋轉(zhuǎn)
  • UISwipeGestureRecongizer: 輕掃
  • UIPanGestureRecognizer: 拖拽

手勢操作的代理方法(UIGestureRecognizerDelegate)

手勢可能發(fā)生的條件,返回NO可以阻止此手勢的發(fā)生或者此手勢不產(chǎn)生任何效果

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

是否允許多個手勢同時發(fā)生

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

UITapGestureRecognier敲擊,點擊手勢

  • 設(shè)置屬性numberOfTapsRequired可以指定需要幾根手指才能觸發(fā)事件
  • numberOfTouchesRequired:可以設(shè)置需要敲擊幾次觸發(fā)事件
 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];  // 設(shè)置代理  tap.delegate = self;  // 設(shè)置點擊次數(shù)觸發(fā)手勢事件  tap.numberOfTapsRequired = 1;  // 設(shè)置需要點擊的手指數(shù)  tap.numberOfTouchesRequired = 1;  [self.image addGestureRecognizer:tap];

UILongPressGestureRecongnizer長按

  • minimumPressDuration設(shè)置長按的最小間隔時間,也就是說按下開始和手指離開時的中間間隔,如果小于這個值則不會被認(rèn)為是長按操作
  • allowableMovement:長按過程中是否允許移動
  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];  // 代理  longPress.delegate = self;  // 設(shè)置最小間隔時間, 手指按下與離開間隔時間  longPress.minimumPressDuration = 1.0;  // 按下過程中允許移動的像素  longPress.allowableMovement = 30;  [self.image addGestureRecognizer:longPress];

UIPinchGestureRecognizer縮放手勢

scale: 設(shè)置縮放比例,相對于原來大小

 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];  // 代理  pinch.delegate = self;  // 設(shè)置縮放比例  pinch.scale = 1.2;  [self.image addGestureRecognizer:pinch];

UIRotationGestureRecognizer旋轉(zhuǎn)手勢

rotation: 旋轉(zhuǎn)弧度,要保證每次都在上一次位置開始旋轉(zhuǎn),而不是回歸初始位置,必須要在動作方法里將此值清零

- (void)setupRotation{  UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];  // 設(shè)置代理  rotation.delegate = self;  [self.image addGestureRecognizer:rotation];}- (void)rotation:(UIRotationGestureRecognizer *)rotation{  // 旋轉(zhuǎn)角度  CGFloat radian = rotation.rotation;  self.image.transform = CGAffineTransformRotate(self.image.transform, radian);  // 復(fù)位,保證每次都是在上一次位置開始轉(zhuǎn),而不是每次都回歸初始位置再轉(zhuǎn)  rotation.rotation = 0;}

UISwipeGestureRecognizer輕掃, 手指按下然后在屏幕上滑動

輕掃分四個方向(上下左右),并且如果要在一個控件上同時添加一個以上的輕掃動作,必須對每個動作添加一個對象。也就是說每個方向的動作對應(yīng)一個對象。

direction: 指定輕掃動作的方向

typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {  UISwipeGestureRecognizerDirectionRight = 1 << 0, // 從左向右  UISwipeGestureRecognizerDirectionLeft = 1 << 1, // 從右向左  UISwipeGestureRecognizerDirectionUp  = 1 << 2, // 從下往上  UISwipeGestureRecognizerDirectionDown = 1 << 3 // 從上往下};
 UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];  // 設(shè)置代理  swipeUp.delegate = self;  // 修改方向, 從下往上  swipeUp.direction = UISwipeGestureRecognizerDirectionUp;  [self.image addGestureRecognizer:swipeUp];  // 添加其他方向手勢  UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];  // 修改方向, 從下往上  swipeDown.direction = UISwipeGestureRecognizerDirectionDown;  [self.image addGestureRecognizer:swipeDown];

UIPanGestureRecognizer拖拽,按下拖動控件操作

注意點:手勢的觸摸點locationInView和手勢的移動點translationInView是不一樣的,前者是用locationInView取得是指手指在當(dāng)前控件中的坐標(biāo),后者表示相對于父view的rect

  UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];  // 設(shè)置代理  pan.delegate = self;  [self.image addGestureRecognizer:pan];
  // 手勢的觸摸點  // CGPoint p = [pan locationInView:self.image];  // 手勢的移動點(每次移動的位移點)  CGPoint transP = [pan translationInView:self.image];  NSLog(@"%f, %f", transP.x, transP.y);  self.image.transform = CGAffineTransformTranslate(self.image.transform, transP.x, transP.y);  // 復(fù)位  [pan setTranslation:CGPointZero inView:self.image];

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 山东省| 青神县| 定结县| 依兰县| 谷城县| 黄骅市| 宁武县| 那坡县| 海宁市| 永川市| 藁城市| 嘉兴市| 岐山县| 昂仁县| 奉化市| 常熟市| 建湖县| 祁门县| 布拖县| 威海市| 武清区| 灌南县| 会理县| 格尔木市| 大悟县| 台南市| 景德镇市| 集安市| 南宁市| 莎车县| 昌图县| 吉首市| 博罗县| 灵山县| 英山县| 逊克县| 湖北省| 常宁市| 丰城市| 雅安市| 屏边|