在UIView上禁用toucesMoved

时间:2011-08-17 06:24:26

标签: objective-c cocoa-touch ios4 uiimageview uiimage

我有一个UIImageView,我在多次单击按钮时添加多个UIImages并在TouchesMoevd的帮助下移动它。 问题是每当我移动UIImage时它会移出UIView。

我希望我的图像只能在UIImageView中移动。

我知道这很容易,但我不知道如何限制特定部分的图像运动???

帮助...

这是我的代码og TouchesMoved Method。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
UITouch *touch = [[event allTouches] anyObject];
imgPimple = (UIImageView*)[touch view];
CGPoint touchLocation = [touch locationInView:[imgPimple superview]];
imgPimple.clipsToBounds = YES;
if ([touch.view isKindOfClass:[UIImageView class]])

    {
        imgPimple.center = touchLocation;
    }
}

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


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

}

1 个答案:

答案 0 :(得分:0)

最后我已经解决了这个问题

解决方案是:在toucesMoved方法中使用此代码

UITouch *touch = [[event allTouches] anyObject];


imgPimple = (UIImageView*)[touch view];


CGPoint touchLocation = [touch locationInView:[image superview]];

if (CGRectContainsPoint(CGRectMake(15, 20, 280, 220), touchLocation)) 
{
    if ([touch.view isKindOfClass:[UIImageView class]])

    {
        imgPimple.center = touchLocation;
    }
}