了解CGAffineTransform交互

了解CGAffineTransform交互

我正在尝试制作一个简单的应用程序,其中“固定” ge的图像在用手指移动后返回其位置。用代码可能可以更好地解释这一点:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     image.transform = CGAffineTransformIdentity;
}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if (CGRectContainsPoint([image frame], [touch locationInView:nil]))
    {
         image.center = [touch locationInView:nil];
    }
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
     if (pin) {
         CGPoint point = image.center;
         CGPoint center = self.view.center;
         //CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 0);
         [UIView beginAnimations:nil context:NULL];
         [UIView setAnimationDuration:0.5];
         image.transform = CGAffineTransformMakeTranslation(center.x - point.x, center.y - point.y);
         //image.transform = CGAffineTransformConcat(image.transform, CGAffineTransformMakeTranslation(center.x - point.x, center.y - point.y));
    [UIView commitAnimations];

    }
}

每次按下图像时,图像都会移动,从而从手指下面移出。我认为这与转换有关。有人能指出我正确的方向吗?

最佳答案

已编辑

我实际上会这样做:

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


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint location = [[touches anyObject] locationInView:[touch view]];
    CGPoint difference = CGPointMake(location.x - image.center.x, location.y - image.center.y);

    image.transform = CGAffineTransformTranslate(image.transform, difference.x, difference.y);
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (pin) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        image.transform = CGAffineTransformIdentity;
        [UIView commitAnimations];
    }
}

关于objective-c - 了解CGAffineTransform交互,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7896148/

10-12 07:24