我正在尝试做一个简单的应用程序,在这个应用程序中,一张被“钉住”的图片在被一个手指移动后会返回到原来的位置。这可能用代码解释得更好:
- (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];
}
}每次我按下图像,它就会移动,这样它就会从我的手指下移出来。我认为这与转换有关。有谁能给我指个方向吗?
发布于 2011-10-26 05:44:14
编辑过的
我实际上是这样做的:
- (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];
}
}发布于 2011-10-26 06:36:38
我觉得你应该直接用
image.transform = CGAffineTransformMakeTranslation(difference.x, difference.y);你这样做的方式是在touchesMoved的每一次迭代中积累越来越多的翻译。我认为center属性并不依赖于变换。
https://stackoverflow.com/questions/7896148
复制相似问题