CAShapeLayer के एनीमेशन को स्केल करने में समस्या [डुप्लिकेट]
मैंने एक स्लाइडर के टूलटिप के लिए सरल पथ बनाया। मैं इसे चेतन करना चाहता था क्योंकि ऐसा प्रतीत होता है कि एक उपयोगकर्ता को पता होगा कि वह इसे खींच सकता है।
लेकिन यह चेतन नहीं करता है:
let upperToolTipPath: CAShapeLayer = CAShapeLayer()
var path = UIBezierPath(roundedRect: CGRect(x: 0,y: 0, width: 65, height: 30), cornerRadius: 3)
path.stroke()
path.move(to: CGPoint(x: 50, y: 10))
path.addLine(to: CGPoint(x: 40, y: 30))
path.addLine(to: CGPoint(x: 15, y: 10))
upperToolTipPath.path = path.cgPath
self.layer.insertSublayer(upperToolTipPath, at: 0)
UIView.animate(
withDuration: 5,
delay: 3,
options: [.repeat, .autoreverse, .curveEaseIn],
animations: {
self.upperToolTipPath.transform = CATransform3DMakeScale(2, 2, 1)
})
क्या आप मुझे एनीमेशन में मदद कर सकते हैं?
जवाब
UIView.animateके लिए है दृश्य एनीमेशन। लेकिन upperToolTipPathएक परत है ; यह एक दृश्य की प्राथमिक परत नहीं है, इसलिए यह केवल लेयर एनीमेशन के साथ एनिमेटेड हो सकता है । इसका मतलब है कि कोर एनिमेशन, यानी कैबसिकैमिनेशन।
इसलिए जिस एनीमेशन को आप बनाना चाहते हैं, उसे बनाने के लिए, आपको कोर एनिमेशन का उपयोग करना होगा।
वैकल्पिक रूप से, आप एक ऐसी आकृति बना सकते हैं जो आकार की परत को उसकी प्राथमिक परत के रूप में होस्ट करती है। तब आप व्यू एनीमेशन का उपयोग कर पाएंगे।
लेकिन मुझे लगता है कि इस मामले में कोर एनीमेशन का उपयोग करना बेहतर है। यहाँ एक बात का उदाहरण है कि मुझे लगता है कि आप करने की कोशिश कर रहे हैं:
let upperToolTipPath = CAShapeLayer()
upperToolTipPath.frame = CGRect(x: 0,y: 0, width: 65, height: 30)
let path = UIBezierPath(roundedRect: CGRect(x: 0,y: 0, width: 65, height: 30),
cornerRadius: 3)
upperToolTipPath.fillColor = UIColor.clear.cgColor
upperToolTipPath.strokeColor = UIColor.black.cgColor
upperToolTipPath.path = path.cgPath
self.layer.insertSublayer(upperToolTipPath, at: 0)
let b = CABasicAnimation(keyPath: #keyPath(CALayer.transform))
b.duration = 5
b.beginTime = CACurrentMediaTime() + 3
b.toValue = CATransform3DMakeScale(2, 2, 1)
b.repeatCount = .greatestFiniteMagnitude
b.autoreverses = true
upperToolTipPath.add(b, forKey:nil)