使用CGAffineTransformTranslate通过触摸在特定区域中移动图像

时间:2013-11-24 22:53:46

标签: ios objective-c cgaffinetransform

我想在屏幕的某个区域移动图像。我不想弄乱框架,因为它会在以后引起碰撞行为的麻烦。到目前为止我得到了这个代码。我不知道如何使用触摸感谢您的帮助

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //CGPoint svpt = [[touches anyObject] locationInView:self.view];
    CGPoint pt = [[touches anyObject] locationInView:self.playerMove];
    CGRect frame = [self.playerMove frame];
    CGAffineTransform CGAT = CGAffineTransformIdentity;
    CGAT = CGAffineTransformTranslate(CGAT, MIN(MAX(frame.origin.x, -10), 240), MIN(MAX(frame.origin.y, 430), 430));
    self.playerMove.transform = CGAT;
}

1 个答案:

答案 0 :(得分:0)

您可以使用平移手势识别器。

UIPanGestureRecognizer *gr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.playerMove addGestureRecognizer:gr];
self.playerMove.userInteractionEnabled = YES;

然后......

- (void)handlePan:(UIPanGestureRecognizer *)gr {

    if (gr.state == UIGestureRecognizerStateBegan || gr.state == UIGestureRecognizerStateChanged) {
        UIView *view = gr.view;
        CGPoint translation = [gr translationInView:view];
        view.transform = CGAffineTransformTranslate(view.transform, translation.x, translation.y);
        [gr setTranslation:CGPointZero inView:view];
    }
}