iOS 仿照nice和in等APP拖动标签在指定范围移动实现

前排附github Demo地址:https://github.com/3KK3/YZYDragTagDemo
因为代码是直接从项目中拖出来的,所以可能有点乱,敬请见谅。

之前项目中有用到添加标签后,拖动 标签可以在 规定范围移动 的需求

预览.png

不过是在touch一系列事件处理的。
最近又碰到这样一个需求,不过这次是用的拖动手势来完成的,代码记录如下,免得以后重复造轮子:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #ffffff}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #ffffff; min-height: 16.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #00afca}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #93c96a}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #c2349b}span.s3 {font-variant-ligatures: no-common-ligatures; color: #00afca}span.s4 {font-variant-ligatures: no-common-ligatures; color: #ffffff}span.s5 {font-variant-ligatures: no-common-ligatures; color: #93c96a}span.s6 {font-variant-ligatures: no-common-ligatures; color: #d28f5a}span.s7 {font-variant-ligatures: no-common-ligatures; color: #8b84cf}span.s8 {font-variant-ligatures: no-common-ligatures; color: #4dbf56}span.s9 {font: 14.0px 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures; color: #4dbf56}

//拖动手势事件处理方法
- (void)panGesAction:(UIPanGestureRecognizer *)panGes {
    
    CGPoint transP = [panGes translationInView: self.superview];
    //标签允许的最小位置处x
    CGFloat minX = _limitRect.origin.x - kBubblyHeight / 2;
    //标签允许的最大位置处x
    CGFloat minY = _limitRect.origin.y - kBubblyHeight / 2;
    
    CGFloat maxX = _limitRect.size.width - kBubblyHeight / 2 + _limitRect.origin.x;
    
    CGFloat maxY = _limitRect.size.height - kBubblyHeight / 2 + _limitRect.origin.y;
    
  //标签默认右向 ,左向需要特殊处理
    switch (_editorItem.tagDirection) {
        case BGTagDirectionLeft: {
            
            minX = kBubblyHeight / 2 - self.width + _limitRect.origin.x;
            
            maxX = _limitRect.origin.x + _limitRect.size.width - self.width + kBubblyHeight / 2;
        }
            break;
            
        default:
            break;
    }
    
    switch (panGes.state) {
            
        case UIGestureRecognizerStateChanged: {
            CGRect newFrame = self.frame;

            if (transP.x > 0 ) { //👉
                newFrame.origin.x = MIN(maxX, transP.x + self.x);
            } else { //👈
                newFrame.origin.x = MAX(minX, transP.x + self.x);
            }
            
            if (transP.y > 0) { //👇
                newFrame.origin.y = MIN(maxY, transP.y + self.y);
            } else { //👆
                newFrame.origin.y = MAX(minY, transP.y + self.y);
            }
            self.frame = newFrame;
            [panGes setTranslation:CGPointZero inView:self.superview];
        }
            break;
            
        case UIGestureRecognizerStateEnded: {

            CGPoint pointCenter = [_pointImgView convertPoint: _pointImgView.center toView: self.superview];
            _editorItem.tagLocation = pointCenter;
            self.tagEditCompletion(_editorItem);
            
        }
            break;
            
        default:
            break;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容