我对Objective-C很新,我希望创建一个相当简单的Jigsaw。我现在只是想制作一个4件式拼图作为学习该方法的一种方式。
目前我在屏幕周围放置了4块,当它们拖到屏幕上的特定位置时,会对齐到那个地方。
我的问题是试图为触摸设置动画。我希望触摸的图像扩展一点但我的语法有问题。我看过Apple提供的MoveMe示例,但在这种情况下它并没有帮助我。下面是代码。我很感激任何帮助。感谢。
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//location of current touch
CGPoint location = [touch locationInView:self.view];
if ([touch view] == image1) {
image1.center = location;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
image1.transform = CGAffineTransformMakeScale(1.5, 1.5);
[UIView commitAnimations];
} else if ([touch view] == image2) {
image2.center = location;
animateFirstTouch:image2;
} else if ([touch view] == image3) {
image3.center = location;
image3.transform = CGAffineTransformMakeScale(1.5, 1.5);
} else if ([touch view] == image4) {
image4.center = location;
image4.transform = CGAffineTransformMakeScale(1.5, 1.5);
}
}
-(void) animateFirstTouch:(UIView *)image {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.view.transform = CGAffineTransformMakeScale(1.5, 1.5);
[UIView commitAnimations];
}
image1的代码有效,图像按比例放大0.5秒。
image2的代码抛出一个错误:表达结果未用于animateFirstTouch:image2。
image3和image4的代码在没有动画的情况下放大图像。
答案 0 :(得分:1)
请参阅此链接中的 Kuldeep 的回答。我认为这对你有用:
iPhone/iOS: How to implement Drag and Drop for subviews of a Scrollview?
此外,另请参阅此链接中 iPhone 的答案:
iPhone App: implementation of Drag and drop images in UIView
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//location of current touch
CGPoint location = [touch locationInView:self.view];
if ([touch view] == image1) {
[self animateFirstTouch:image1 withLocation:location];
} else if ([touch view] == image2) {
[self animateFirstTouch:image2 withLocation:location];
} else if ([touch view] == image3) {
[self animateFirstTouch:image3 withLocation:location];
} else if ([touch view] == image4) {
[self animateFirstTouch:image4 withLocation:location];
}
}
-(void) animateFirstTouch:(UIImageView *)image withLocation:(CGPoint)location {
image.center = location;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
image.transform = CGAffineTransformMakeScale(1.5, 1.5);
[UIView commitAnimations];
}
希望这会对你有所帮助。
如果您需要更多帮助,请与我联系。