使用UIPanGestureRecognizer移动图片时,图片始终会移回原点

时间:2015-05-18 05:34:59

标签: ios xcode uipangesturerecognizer

我尝试以编程方式实现平移手势,但是当我尝试它时,uiview总是在移动过程中移回到superview内的原点。所以实际上它从原点跳到了新的位置。 有谁知道这是什么问题?

代码:

- (void)setGesture
{
    self.userInteractionEnabled = YES;
        UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(detectPan:)];
        self.gestureRecognizers = @[panRecognizer];


}

- (void) detectPan:(UIPanGestureRecognizer *) penGes
{
    if ((penGes.state == UIGestureRecognizerStateChanged) ||
        (penGes.state == UIGestureRecognizerStateEnded))
    {

        CGPoint translation = [penGes translationInView:self.superview];
        self.center = CGPointMake(self.lastLocation.x + translation.x,
                              self.lastLocation.y + translation.y);

       [penGes setTranslation:CGPointZero inView:self];
    }
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Promote the touched view
    [self.superview bringSubviewToFront:self];

    // Remember original location
    self.lastLocation = self.center;
}

1 个答案:

答案 0 :(得分:0)

我认为您的翻译与Autolayout约束相冲突 请查看this answermatt关于转换和Autolayout的信息。