iOS:UIView动画忽略了延迟

时间:2014-05-09 08:50:36

标签: ios uiviewanimation

我遇到一个非常简单的UIView动画问题:

- (void)showView:(CGFloat)delay
{
    [UIView animateWithDuration:1.0 delay:delay options:0 animations:^{

        // self.alpha = 0.5; // Works OK with delay
        self.frame = CGRectMake(100, 100, 100, 30); // delay is ignored
    } completion:nil];
}

delay可以设置为1000,并且仍会立即查看动画。但不知怎的,它可以与alpha一起使用(不设置frame)。

4 个答案:

答案 0 :(得分:0)

可能正在为您设置框架,可能是由superview执行的布局或此视图本身。你还没有说过你如何添加这个观点或者它做了什么,所以很难给出具体的建议,但总的来说:

  • 不要让视图控制自己的框架 - 这是超级视图或视图控制器的责任。视图可以具有首选大小,但其余视图在外部。
  • 通常情况下,更好地设置边界和/或中心而不是框架,尤其是如果您忽略第一个点。
  • 对于演示文稿类型动画,最好为变换设置动画 - 转换或缩放,具体取决于动画的内容。

答案 1 :(得分:0)

[self performSelector:@selector(animateMe) withObject:nil afterDelay:1000];

如果selfviewController,那么您应该使用self.view.frame代替self.frame。如果selfview,那么您应该使用self.frame

- (void)animateMe {

    [UIView animateWithDuration:1.0f animations:^{

        // self.alpha = 0.5; // Works OK with delay
        self.frame = CGRectMake(100, 100, 100, 30); // delay is ignored
    } completion:^(BOOL finished) {

        NSLog(@"Done");
    }];            
}

答案 2 :(得分:0)

此答案使用Swift,特别是4.2版,并在Xcode 10游乐场中进行了测试。这实际上是我第一次尝试制作动画,并且总体上我对iOS感到非常绿色,但这就是我解决了delay未被观察到的问题的方法。

import UIKit
import PlaygroundSupport

// Set up the main view
let liveViewFrame = CGRect(x: 0, y: 0, width: 500, height: 500)
let liveView = UIView(frame: liveViewFrame)
liveView.backgroundColor = .white

// Show the live view using the main view
PlaygroundPage.current.liveView = liveView

// Set up a smaller view for animation
let smallFrame = CGRect(x: 0, y: 0, width: 100, height: 100)
let square = UIView(frame: smallFrame)
square.backgroundColor = .purple

// Add the square to the live view ...
liveView.addSubview(square)

// ... and create an alias for it to allow delays to work.
// Calling square directly won't allow delays,
// giving it or the liveView subview an alias directly fails,
// and unwrapping (? or !) appears to fail as well.
// So, just call like `alias?.backgroundColor` ...
let squareView = square.superview?.subviews[0]

UIView.animate(withDuration: 1.5, delay: 1.0, options: [.repeat], animations: {
    //square.backgroundColor = .orange // this doesn't respect the delay arg
    squareView?.backgroundColor = .orange
    squareView?.frame = CGRect(x: 150, y: 150, width: 200, height: 200)
    squareView?.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
}, completion: nil)

这可能无法直接回答问题,但是我相信类似的逻辑可以在相关情况下用于解决方法。

答案 3 :(得分:0)

代替

UIView.animate(
    withDuration: 0.2, 
    delay: 1, 
    options: .curveLinear, 
    animations: {
        self.someButton.isHidden = false
    })
}

只做:

DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
    UIView.animate(withDuration: 0.2, 
        delay: 0, 
        options: .curveLinear, 
        animations: {
        self.someButton.isHidden = false
    })
}