视图走出视图边界

时间:2012-02-21 07:09:58

标签: iphone objective-c ipad ios5 gestures

我有一个背景视图(UIImageView),我正在添加一个视图(UIView)。 此contentView(UIView)包含UIPanGestureRecognizer。

我通过以下代码将UIPanGestureRecognizer提供给contentView:

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(movePhoto:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[contentView addGestureRecognizer:panRecognizer];
[panRecognizer release];

然后我将通过以下代码将此contentView添加到Background View(UIImageView):

[imgBG addSubview:contentView]; // here imgBG is Background view.

但是当我移动这个内容时,它就会脱离imgBG。

bellow是移动contentView的方法

    [[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];

[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) 
{
    firstX = [[sender view] center].x;
    firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];

CGFloat finalX = [sender view].center.x;
CGFloat finalY = [sender view].center.y;
[[sender view] setCenter:CGPointMake(finalX, finalY)];

我认为,这里的contentView应该保留在imgBG中,因为它是imgBG的子视图。

由于UIPanGestureRecognizer,请告知我代码中是否存在问题。

提前感谢。

1 个答案:

答案 0 :(得分:2)

我必须这样做:

[imgBG setClipsToBounds:YES];