美文网首页
长按和拖动手势 iOS

长按和拖动手势 iOS

作者: woo_5857 | 来源:发表于2021-09-10 13:27 被阅读0次

    UIPanGestureRecognizer *thisPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(thisViewPanClick:)];

            [self addGestureRecognizer:thisPan]; //拖动

            UILongPressGestureRecognizer *longPan = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(thisViewLongPanClick:)];

            [self addGestureRecognizer:longPan];//长按

    - (void)thisViewLongPanClick:(UILongPressGestureRecognizer *)pan {

        CGPoint currentPoint = [pan locationInView:self];

        CGPoint trPoint = CGPointMake(currentPoint.x - touchOffset.x, currentPoint.y -  touchOffset.y);

        touchOffset = currentPoint;

        switch (pan.state) {

            case UIGestureRecognizerStateBegan:

            {

                [self getCurSelectBut:currentPoint];

                self.hidden = YES;

                [[ASMagnifierManger sharedInstance] magnifierPoint_Began:CGPointMake(self.selectBut.width/2, self.selectBut.height/2) withView:self.selectBut];

                //振动

                AudioServicesPlaySystemSound(1519);

                self.hidden = NO;

                break;

            }

            case UIGestureRecognizerStateChanged:

            {

                [self moveTouchPoint:trPoint];

                [[ASMagnifierManger sharedInstance] magnifierPoint_Moved:CGPointMake(self.selectBut.width/2, self.selectBut.height/2) withView:self.selectBut];

                break;

            }

            case UIGestureRecognizerStateEnded:

            case UIGestureRecognizerStateCancelled:

            case UIGestureRecognizerStateFailed:

            {

                self.selectBut = nil;

                [[ASMagnifierManger sharedInstance] magnifier_Ended];

                if (self.modifyCallBlock) {

                    self.modifyCallBlock(YES);

                }

                break;

            }

            default:

                break;

        }

    }

    - (void)thisViewPanClick:(UIPanGestureRecognizer *)pan {

        CGPoint currentPoint = [pan locationInView:self];

        CGPoint trPoint = [pan translationInView:self];

        switch (pan.state) {

            case UIGestureRecognizerStateBegan:

            {

                [self getCurSelectBut:currentPoint];

                self.hidden = YES;

                [[ASMagnifierManger sharedInstance] magnifierPoint_Began:CGPointMake(self.selectBut.width/2, self.selectBut.height/2) withView:self.selectBut];

                //振动

                AudioServicesPlaySystemSound(1519);

                self.hidden = NO;

                break;

            }

            case UIGestureRecognizerStateChanged:

            {

                [self moveTouchPoint:trPoint];

                [[ASMagnifierManger sharedInstance] magnifierPoint_Moved:CGPointMake(self.selectBut.width/2, self.selectBut.height/2) withView:self.selectBut];

                break;

            }

            case UIGestureRecognizerStateEnded:

            case UIGestureRecognizerStateCancelled:

            case UIGestureRecognizerStateFailed:

            {

                self.selectBut = nil;

                [[ASMagnifierManger sharedInstance] magnifier_Ended];

                if (self.modifyCallBlock) {

                    self.modifyCallBlock(YES);

                }

                break;

            }

            default:

                break;

        }

        [pan setTranslation:CGPointZero inView:self];

    }

    - (void)getCurSelectBut:(CGPoint)currentPoint {

        UIButton *selectBut = _pointA;

        CGFloat minLent = [self getLinethPointOne:currentPoint pointTwo:_pointA.center];

        CGFloat nlent = [self getLinethPointOne:currentPoint pointTwo:_pointB.center];

        if (nlent < minLent) {

            minLent = nlent;

            selectBut = _pointB;

        }

        nlent = [self getLinethPointOne:currentPoint pointTwo:_pointC.center];

        if (nlent < minLent) {

            minLent = nlent;

            selectBut = _pointC;

        }

        nlent = [self getLinethPointOne:currentPoint pointTwo:_pointD.center];

        if (nlent < minLent) {

            minLent = nlent;

            selectBut = _pointD;

        }

        nlent = [self getLinethPointOne:currentPoint pointTwo:_pointE.center];

        if (nlent < 60) {

            minLent = nlent;

            selectBut = _pointE;

        }

        nlent = [self getLinethPointOne:currentPoint pointTwo:_pointF.center];

        if (nlent < 60) {

            minLent = nlent;

            selectBut = _pointF;

        }

        nlent = [self getLinethPointOne:currentPoint pointTwo:_pointG.center];

        if (nlent < 60) {

            minLent = nlent;

            selectBut = _pointG;

        }

        nlent = [self getLinethPointOne:currentPoint pointTwo:_pointH.center];

        if (nlent < 60) {

            minLent = nlent;

            selectBut = _pointH;

        }

        self.selectBut = selectBut;

    }

    //测量两点的直线距离

    - (CGFloat)getLinethPointOne:(CGPoint)oneP pointTwo:(CGPoint)twoP {

        CGFloat lenth = sqrt(pow(oneP.x - twoP.x, 2) + pow(oneP.y - twoP.y, 2));

        return lenth;

    }

    相关文章

      网友评论

          本文标题:长按和拖动手势 iOS

          本文链接:https://www.haomeiwen.com/subject/qjtawltx.html