拖动时旋转视图

时间:2014-01-29 05:40:32

标签: ios objective-c

我想在拖动时稍微旋转UIView。我设法使用UIPanGestureRecognizer进行拖动。我也尝试使用CGAffineTransformRotate旋转它并且它有效。但是,当我同时应用拖动和旋转时,UIView仅旋转,并且不会拖动超级视图。以下是我在手势处理程序中所做的事情:

CGPoint translation = [gesture translationInView:self.view];
    // to drag
    gesture.view.center = CGPointMake(gesture.view.center.x + translation.x, gesture.view.center.y + translation.y);
    // to rotate
    gesture.view.transform = CGAffineTransformRotate(gesture.view.transform, degreesToRadians(translation.x)/4);
    // return to default so that it doesn't accumulate
    [gesture setTranslation:CGPointMake(0, 0) inView:self.view];

我该如何解决这个问题?

修改 这是整个代码:

- (void)handlePan:(UIPanGestureRecognizer*)gesture{
if (gesture.state == UIGestureRecognizerStateBegan) {
    // Save initial post center for snapping
    point = CGPointMake(gesture.view.center.x, gesture.view.center.y);
} else if (gesture.state == UIGestureRecognizerStateChanged){
    // Translate user movement across the screen to dragging coordinates
    CGPoint translation = [gesture translationInView:self.view];

    gesture.view.center = CGPointMake(gesture.view.center.x + translation.x, gesture.view.center.y + translation.y);

    CGAffineTransform rotate = CGAffineTransformRotate(gesture.view.transform, degreesToRadians(translation.x)/4);

    gesture.view.transform = rotate;//CGAffineTransformConcat(translate, rotate);

    [gesture setTranslation:CGPointMake(0, 0) inView:self.view];


} else if (gesture.state == UIGestureRecognizerStateEnded){
    // Animate snap back to place
    [UIView animateWithDuration:0.2 animations:^{
        gesture.view.transform = CGAffineTransformIdentity;
       // [self.imagePost setCenter:point];
    }];
}

}

1 个答案:

答案 0 :(得分:1)

尝试添加此方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (![gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && ![otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
    return YES;
}

return NO;

}