2014-04-17 50 views
0

我試圖用touch拖動UITextView對象!但我有一個問題,我需要用戶觸摸custom view時,觸摸事件開始,但觸摸方法只適用於self.view。這裏是我的代碼:使用UITextView拖放

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSLog(@"touch began "); 

    UITouch *touch = [touches anyObject]; 

    startingX = [touch locationInView:_captureView].x; 
    startingY = [touch locationInView:_captureView].y; 
} 




- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 



    CGPoint currentPoint = _mText.frame.origin; 

    float xForView, yForView; 
    UITouch *touch = [touches anyObject]; 
    float newX = [touch locationInView:_captureView].x; 
    float deltaX; 


    if(startingX > newX){ 
     deltaX = startingX - newX; 
     xForView = currentPoint.x - deltaX; 
    } else if(newX > startingX){ 
     deltaX = newX - startingX; 
     xForView = currentPoint.x + deltaX; 
    } else xForView = currentPoint.x; 


    float newY = [touch locationInView:_captureView].y; 
    float deltaY; 

    if(startingY > newY){ 
     deltaY = startingY - newY; 
     yForView = currentPoint.y - deltaY; 
    } else if(newY > startingY){ 
     deltaY = newY - startingY; 
     yForView = currentPoint.y + deltaY; 
    } else yForView = currentPoint.y; 



    CGRect newFrame = CGRectMake(xForView, yForView, _mText.frame.size.width, _mText.frame.size.height); 
    _mText.frame = newFrame; 

    startingX = newX; 
    startingY = newY; 
} 

我的TextView不是編輯所有觀點都是userInteractionEnabled

+0

touchesBegan,touchesMoved方法在你繼承UIView的時候被調用。你必須繼承查看 –

+0

@SunnyShah對不起,我不明白! ? –

回答

0

撰寫您touches began方法和您的自定義文本視圖中touches moved方法,並嘗試調整框架。

+0

自定義視圖只是一個UIView而不是ViewController! –