【问题标题】:Move image from left to right将图像从左向右移动
【发布时间】:2011-05-12 14:10:42
【问题描述】:

在视图加载后,在我的应用程序中,图像应该从左到右出现。 我在viewdidloadviewwillappear 中编写此代码:

       UIImage *image = [UIImage imageNamed:@"PG05(REV).jpg"];
       UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
       for (int i=-1024; i<=0; i++) {
               NSLog(@"i: %d",i);
       sleep(5);
       imageView.frame = CGRectMake(i,0, 1024, 768);
               NSLog(@"done");
       [self.view addSubview:imageView];

       }
       [imageView release];

但问题是,在加载视图之前,它执行上述代码而不是加载视图,因此看起来视图已加载并且静态图像在那里。 但我的要求是第一个视图完全加载,然后图像应该从左到右显示。

请帮帮我。

【问题讨论】:

    标签: iphone objective-c ipad


    【解决方案1】:

    首先,您不应该在 for 循环中调用 -(void)addSubview:。你只需要调用一次。 其次,如果你想从左到右为你的 imageView 设置动画,UIView class 为这个使用块提供了很好的功能。

    您的代码应如下所示:

    UIImage *image = [UIImage imageNamed:@"PG05(REV).jpg"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(-1024, 0, 1024, 768);
    [self.view addSubview:imageView];
    [imageView release]; //Your imageView is now retained by self.view
    
    //Animation
    [UIView animateWithDuration:2.0
                     animations:^(void) {
                        imageView.frame = CGRectMake(0, 0, 1024, 768);
                     }];
    

    animateWithDuration: 方法将为您处理动画计时器,只需根据您的需要设置持续时间参数即可。

    【讨论】:

    • 您好,非常感谢。你能告诉我在touchBegan(触摸图像)上停止这个动画的条件/代码是什么。
    【解决方案2】:

    您可以使用函数并使用计时器方法调用它。

    timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(moveImage) userInfo:nil repeats:NO];
    
    -(void)moveImage
    {
    //Your condition
    }
    

    【讨论】:

      【解决方案3】:

      这是 UIView 动画的工作。

      把你的 imageView 放在左边 -1024,然后走:

      [imageView animateWithDuration:5 animations:^{
          [imageView.frame = CGRectMake(0,0, 1024, 768)];
      }];
      

      哒哒!图像向右滑动的 5 秒动画。

      查看 UIView 的动画方法(其中 UIImageView 是子类): http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html

      【讨论】:

        猜你喜欢
        • 2013-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-01
        • 2016-05-16
        相关资源
        最近更新 更多