iOS OC 兩種方法限制拖拽手勢(shì)的范圍

UIGestureRecognizer是一個(gè)定義基本手勢(shì)的抽象類,具體包含:
1、拍擊UITapGestureRecognizer (任意次數(shù)的拍擊)
2、向里或向外捏UIPinchGestureRecognizer (用于縮放)
3、搖動(dòng)或者拖拽UIPanGestureRecognizer (拖動(dòng))
4、擦碰UISwipeGestureRecognizer (以任意方向)
5、旋轉(zhuǎn)UIRotationGestureRecognizer (手指朝相反方向移動(dòng))
6、長(zhǎng)按UILongPressGestureRecognizer (長(zhǎng)按)
本文主要使用到的是拖拽手勢(shì):UIPanGestureRecognizer

限制方法:

-(void)doMoveAction:(UIPanGestureRecognizer *)recognizer{
    // Figure out where the user is trying to drag the view. 
    CGPoint translation = [recognizer translationInView:self.view];
    CGPoint newCenter = CGPointMake(recognizer.view.center.x+ translation.x,
                                    recognizer.view.center.y + translation.y);//    限制屏幕范圍:
    newCenter.y = MAX(recognizer.view.frame.size.height/2, newCenter.y);
    newCenter.y = MIN(self.view.frame.size.height - recognizer.view.frame.size.height/2,  newCenter.y);
    newCenter.x = MAX(recognizer.view.frame.size.width/2, newCenter.x);
    newCenter.x = MIN(self.view.frame.size.width - recognizer.view.frame.size.width/2,newCenter.x);
    recognizer.view.center = newCenter;
    [recognizer setTranslation:CGPointZero inView:self.view];
}

也可以使用UITouch來(lái)實(shí)現(xiàn):
當(dāng)手指接觸到屏幕,不管是單點(diǎn)觸摸還是多點(diǎn)觸摸,事件都會(huì)開始,直到用戶所有的手指都離開屏幕。期間所有的UITouch對(duì)象都被包含在UIEvent事件對(duì)象中,由程序分發(fā)給處理者。事件記錄了這個(gè)周期中所有觸摸對(duì)象狀態(tài)的變化。
限制方法如下:

BOOL isMove;
CGPoint legend_point;
-(void)touchesBegan:(NSSet<uitouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    isMove = NO;
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    if (CGRectContainsPoint(V2.frame, point)) {
        legend_point = [touch locationInView:V2];
        isMove = YES;
    }
}
 
-(void)touchesMoved:(NSSet<uitouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
    if (!isMove) {
        return;
    }
    @autoreleasepool {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self.view];
        //轉(zhuǎn)化成相對(duì)的中心
        point.x += V2.frame.size.width/2.0f - legend_point.x;
        point.y += V2.frame.size.height/2.0f - legend_point.y;
//        限制范圍
        if (point.x < V2.frame.size.width / 2.0f) {
            point.x = V2.frame.size.width / 2.0f;
        }
        if (point.y < V2.frame.size.height / 2.0f) {
            point.y = V2.frame.size.height / 2.0f;
        }
 
        if (point.x > self.view.frame.size.width - V2.frame.size.width / 2.0f) {
            point.x = self.view.frame.size.width - V2.frame.size.width / 2.0f;
        }
        if (point.y > self.view.frame.size.height - V2.frame.size.height / 2.0f) {
            point.y = self.view.frame.size.height - V2.frame.size.height / 2.0f;
        }
        V2.center = point;
 
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容