遇到UITextView拖放问题

时间:2014-04-17 09:00:27

标签: ios objective-c uitextview

我试图用触摸拖动UITextView对象!但我有一个问题,我需要在用户触摸custom view时触摸事件开始,但触摸方法仅适用于self.view。这是我的代码:

- (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

1 个答案:

答案 0 :(得分:0)

将您的touches began方法和touches moved方法写在自定义文本视图中,然后尝试调整框架。