我遇到一个非常简单的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
)。
答案 0 :(得分:0)
可能正在为您设置框架,可能是由superview执行的布局或此视图本身。你还没有说过你如何添加这个观点或者它做了什么,所以很难给出具体的建议,但总的来说:
答案 1 :(得分:0)
[self performSelector:@selector(animateMe) withObject:nil afterDelay:1000];
如果self
为viewController
,那么您应该使用self.view.frame
代替self.frame
。如果self
为view
,那么您应该使用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
})
}