我正在尝试创建一个简单的应用程序,其中被“固定”的图像在被手指移动后返回到其位置。使用代码可能会更好地解释这一点:
- (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];
}
}
每次按下图像,它都会移动,使其从我的手指下移出。我认为它与转换有关。有人可以指出我正确的方向吗?
答案 0 :(得分:3)
我认为你应该使用
image.transform = CGAffineTransformMakeTranslation(difference.x, difference.y);
你正在这样做的方式,你每次迭代touchesMoved都会积累越来越多的翻译。中心属性不依赖于我认为的转变。
答案 1 :(得分:1)
EDITED
我实际上是这样做的:
- (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];
}
}