我有100张图片(a400e3x_0.png,.....,a400e3x_98.png,a400e3x_99.png)。 这些图像根据UIImageView上的滑动而变化,就像用户可以控制的动画一样。 问题是当用户停止平移然后再次启动时,动画将返回到图像0.我需要在用户停止平移时保存位置,我可以这样做。但是当我添加它时,添加总是在那里。示例:我在图像16处停止平移,然后从16开始,当(curX%100)为零时,它仍然会添加16。你可以帮我解决这个问题。
- (IBAction)PanGesAction:(id)sender
{
CGPoint translation = [_PanGes translationInView:self.view];
int curX = (int)translation.x;
addition = (curX%100)+Savedif;
NSLog(@"%d", addition);
[_ImageV2 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"a400e3x_%d.png", addition]]];
if (_PanGes.state == UIGestureRecognizerStateEnded)
{
NSLog(@"End");
Savedif = addition;
//When user end
}
if (_PanGes.state == UIGestureRecognizerStateBegan)
{
NSLog(@"Began");
//When user begin
}
}
答案 0 :(得分:1)
当一个新手势开始时,您有机会编辑它将要发送给您的值,因此我们可以将您的起始值转移到手势并保持简单:
- (IBAction)PanGesAction:(id)sender
{
if (_PanGes.state == UIGestureRecognizerStateBegan)
{
NSLog(@"Began");
//When user begin
[_PanGes setTranslation:(CGPoint){Savedif, 0} inView:self.view];
Savedif = 0;
}
CGPoint translation = [_PanGes translationInView:self.view];
int curX = (int)translation.x;
addition = (curX%100);
NSLog(@"%d", addition);
[_ImageV2 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"a400e3x_%d.png", addition]]];
if (_PanGes.state == UIGestureRecognizerStateEnded)
{
NSLog(@"End");
Savedif = addition;
//When user end
}
}
答案 1 :(得分:-1)
有一种方法,你不必保存任何东西,
在transform
,
UIView
属性
试试这段代码:
- (void)handlePanGesture:(UIPanGestureRecognizer *)gesture
{
UIView *imageView = gesture.view;
if (gesture.state == UIGestureRecognizerStateChanged) {
CGPoint transition = [gesture translationInView:gesture.view];
imageView.transform = CGAffineTransformMakeTranslation(imageView.transform.tx + transition.x, imageView.transform.tx + transition.y);
[gesture setTranslation:CGPointZero inView:gesture.view];
}
else if (gesture.state == UIGestureRecognizerStateEnded) {
imageView.center = CGPointMake(imageView.center.x + imageView.transform.tx, imageView.center.y + imageView.transform.ty);
imageView.transform = CGAffineTransformIdentity;
}
}
imageView的位置始终为:
imageView.center.x+image.transform.tx
imageView.center.y+image.transform.ty
你可以据此采取行动。