zoukankan      html  css  js  c++  java
  • iOS UIPanGestureRecognizer



    UIPanGestureRecognizer负责拖动手势。
    #import "MyView.h"
    
    
    @interface MyView()
    
    //@property(assign, nonatomic) CGPoint startTouchPosition;
    @property(assign, nonatomic) CGRect initFrame;
    
    @end
    
    @implementation MyView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.initFrame = frame;
            [self setGesture];
            self.backgroundColor = [UIColor grayColor];
        }
        return self;
    }
    
    - (void)setGesture{
        UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDrag:)];
        [self addGestureRecognizer:panGR];
    }
    
    - (void)handleDrag:(UIPanGestureRecognizer*) recgonizer{
        CGPoint point =
        [recgonizer translationInView:nil];//不传参数表示坐标系是当前窗口(指当前的UIWindow,不是指view)
        CGFloat dltX = point.x;
        CGFloat dltY = point.y;
        [recgonizer setTranslation:CGPointZero inView:nil];//由于translationInView表示此次移动的位移,如果不重置就会越来越大,导致拖动越来越快。
    self.center = CGPointMake(self.center.x + dltX, self.center.y + dltY); NSLog(@"拖动中"); } @end

    或者可以这样简单实现handleDrag方法:

    - (void)handleDrag:(UIPanGestureRecognizer*) recgonizer{
        CGPoint point =
        [recgonizer locationInView:nil];
        self.center = point;
        NSLog(@"拖动中");
    }

    因为locationInView是返回坐标的,不传参数表示坐标系就是当前窗口。

  • 相关阅读:
    Reverse Integer
    Same Tree
    BST(Binary Search Tree)
    Maximum Depth of Binary Tree
    Single Number
    Computer System Basic Concept(2)
    破解企业QQ对个人QQ登陆的限制(原创)
    鸟哥的Linux私房菜.基础学习篇(摘要)(持续更新)
    Linux系列书籍
    【总结】关于彩虹表学习阶段性总结
  • 原文地址:https://www.cnblogs.com/FdWzy/p/14169681.html
Copyright © 2011-2022 走看看