【问题标题】:Image sequence with pan gesture algorthm带有平移手势算法的图像序列
【发布时间】:2013-09-22 12:58:14
【问题描述】:

我有 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

    }
}

【问题讨论】:

  • 您已经检测到UIGestureRecognizerStateBegan,为什么不使用它来决定如何管理您的addition

标签: ios objective-c xcode algorithm uiimageview


【解决方案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
    }
}

【讨论】:

  • 谢谢!我已经坚持了好几个小时了!
【解决方案2】:

有一种方法你不必保存任何东西,

UIView 中引入transform 属性,

试试这个代码:

- (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

你可以采取相应的行动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 2016-09-27
    相关资源
    最近更新 更多