UIView.transition 动画 只用一次,第二次就没了
UIView.transition animation It only works once, a second time the picture just disappears
如何让动画保持一致?
func addBlurTo(_ image: UIImage) -> UIImage? {
guard let ciImg = CIImage(image: image) else { return nil }
let blur = CIFilter(name: "CIGaussianBlur")
blur?.setValue(ciImg, forKey: kCIInputImageKey)
blur?.setValue(1 , forKey: kCIInputRadiusKey)
if let outputImg = blur?.outputImage {
return UIImage(ciImage: outputImg)
}
return nil
}
对于按钮:
@IBAction func Button7(_ sender: UIButton) {
UIView.transition(with: imageView, duration: 1, options: .transitionCrossDissolve, animations: {
guard let testImage = self.imageView.image else {return}
self.imageView.image = self.addBlurTo(testImage)
}, completion: nil)
}
您需要将函数稍微更改为以下内容,它应该可以工作。问题是在不使用上下文的情况下从 ciImage 转换为 UIImage。看这里:
If the UIImage object was initialized using a CIImage object, the value of the property is NULL.
这里:
func addBlurTo(_ image: UIImage) -> UIImage? {
guard let ciImg = CIImage(image: image) else { return nil }
let blur = CIFilter(name: "CIGaussianBlur")
blur?.setValue(ciImg, forKey: kCIInputImageKey)
blur?.setValue(1 , forKey: kCIInputRadiusKey)
if let outputImg = blur?.outputImage {
let context = CIContext()
guard let cgOutput = context.createCGImage(outputImg, from: outputImg.extent) else { return nil }
return UIImage(cgImage: cgOutput)
}
return nil
}
如何让动画保持一致?
func addBlurTo(_ image: UIImage) -> UIImage? {
guard let ciImg = CIImage(image: image) else { return nil }
let blur = CIFilter(name: "CIGaussianBlur")
blur?.setValue(ciImg, forKey: kCIInputImageKey)
blur?.setValue(1 , forKey: kCIInputRadiusKey)
if let outputImg = blur?.outputImage {
return UIImage(ciImage: outputImg)
}
return nil
}
对于按钮:
@IBAction func Button7(_ sender: UIButton) {
UIView.transition(with: imageView, duration: 1, options: .transitionCrossDissolve, animations: {
guard let testImage = self.imageView.image else {return}
self.imageView.image = self.addBlurTo(testImage)
}, completion: nil)
}
您需要将函数稍微更改为以下内容,它应该可以工作。问题是在不使用上下文的情况下从 ciImage 转换为 UIImage。看这里:
If the UIImage object was initialized using a CIImage object, the value of the property is NULL.
这里:
func addBlurTo(_ image: UIImage) -> UIImage? {
guard let ciImg = CIImage(image: image) else { return nil }
let blur = CIFilter(name: "CIGaussianBlur")
blur?.setValue(ciImg, forKey: kCIInputImageKey)
blur?.setValue(1 , forKey: kCIInputRadiusKey)
if let outputImg = blur?.outputImage {
let context = CIContext()
guard let cgOutput = context.createCGImage(outputImg, from: outputImg.extent) else { return nil }
return UIImage(cgImage: cgOutput)
}
return nil
}