我正在尝试在iOS应用程序中拖动CALayer
。
一旦我更改了其位置属性,它会尝试动画到新位置并在整个区域闪烁:
layer.position = CGPointMake(x, y)
我该如何立即移动CALayers
?我似乎无法理解核心动画API。
我正在尝试在iOS应用程序中拖动CALayer
。
一旦我更改了其位置属性,它会尝试动画到新位置并在整个区域闪烁:
layer.position = CGPointMake(x, y)
我该如何立即移动CALayers
?我似乎无法理解核心动画API。
。
[CATransaction begin];
[CATransaction setValue: (id) kCFBooleanTrue forKey: kCATransactionDisableActions];
layer.position = CGPointMake(x, y);
[CATransaction commit];
[UIView performWithoutAnimation:<#^(void)actionsWithoutAnimation#>];
- k06aSwift 3扩展:
extension CALayer {
class func performWithoutAnimation(_ actionsWithoutAnimation: () -> Void){
CATransaction.begin()
CATransaction.setValue(true, forKey: kCATransactionDisableActions)
actionsWithoutAnimation()
CATransaction.commit()
}
}
使用方法:
CALayer.performWithoutAnimation(){
someLayer.position = newPosition
}
@noescape
属性是有用的,可以允许(并明确)在块中不需要使用self
。 - Andrew你还可以使用方便的函数
[CATransaction setDisableActions:YES]
同样也要。
注意:务必阅读 Yogev Shelly 的评论,以了解可能发生的任何问题。
正如其他人建议的那样,您可以使用CATransaction
。
问题出现在CALayer默认的隐式动画持续时间为0.25秒。
因此,在我看来,比setDisableActions
更简单的替代方法是使用setAnimationDuration
,并将值设置为0.0
。
[CATransaction begin];
[CATransaction setAnimationDuration:0.0];
layer.position = CGPointMake(x, y);
[CATransaction commit];
以下是 Swift 4 的解决方案,明确指定动画持续时间:
extension CALayer
{
class func perform(withDuration duration: Double, actions: () -> Void) {
CATransaction.begin()
CATransaction.setAnimationDuration(duration)
actions()
CATransaction.commit()
}
}
用法...
CALayer.perform(withDuration: 0.0) {
aLayer.frame = aFrame
}