代码之家  ›  专栏  ›  技术社区  ›  jamesh

iOS上3个图像之间的交叉衰落

  •  0
  • jamesh  · 技术社区  · 14 年前

    我正在尝试实现一个可点击的图像,它能够在三个或更多的图像之间反复循环。一旦设置了循环,我不需要与图像交互。循环是基于时间的,而不是基于事件的。图像应该平滑地从一个过渡到另一个。

    解决这个问题最好的方法是什么?

    My first approach was a finite state machine in a single method, and chain UIView 动画块一起使用该方法的选择器。失败了 [button setImage:nextImage forState:UIControlStateNormal] 未设置动画。

    My second approach was to look at CABasicAnimation 和层。这似乎是一种更低级的方法,但我看不到将转换链接在一起的方法(例如,在完成此操作后设置下一个操作)。

    我的直觉是,使用多个按钮并在它们之间淡入淡出是完全错误的,但我不确定下一步该去哪里。

    How do I smoothly fade between images in a UIButton ?

    How do I chain those transitions together?

    什么是正确的方法?

    编辑: 图像I1、I2、I3需要按顺序I1->I2->I3->I2->I1->I2->I3等

    3 回复  |  直到 13 年前
        1
  •  4
  •   Srikar Appalaraju Tonetel    13 年前

    我的第一个想法是 UIImageView.animationImages 但我不认为它支持褪色。

    不要使用 UIButton 要显示图像,只需在处理图形的其他对象上使其成为一个不可见的按钮。

    制作动画的一个简单方法是使用两个图像视图:将“淡入淡出”图像视图移到前面( -[UIView bringSubviewToFront:] 将其alpha设置为0,然后将其alpha动画化为1。

        2
  •  0
  •   jamesh    14 年前

    我的解决方案:

    • 有一个 UIButton 一个容器 UIView .
    • 容器 UIVIEW 包含所有的图像 UIImageView 儿童视角。
    • 在uiviewcontroller的viewdidAppear方法中启动动画,其中所有子视图的alpha都设置为1.0。然后将第0个ImageView Alpha设置为1.0。
    • 动画完成后,回想相同的方法,设置延迟。
    • and takes care of animating to the next image.

    I'm not sure if it's correct way of doing things, but it seemed a lot neater than the alternative -

    • 所有图像都可以在接口生成器中设置,而不会污染代码中的图像名称。
    • 该方法是自给自足的,内存管理是免费的。
        3
  •  -3
  •   Luke    14 年前

    我相当肯定这样的事情会起作用:

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
    
    [button setImage:whicheverImage forState:UIControlStateNormal];
    
    [UIView commitAnimations];
    

    It should fade through smoothly if you give it a duration of maybe 0.5 (seconds).