Animacja ścieżki Beziera narysowanej w drawRect () Swift
I have this shape that i draw in drawRect()
var rectanglePath = UIBezierPath()
override func drawRect(rect: CGRect) {
rectanglePath = UIBezierPath(rect: self.bounds)
rectanglePath.fillWithBlendMode(kCGBlendModeMultiply, alpha: 0.7)
layer.shouldRasterize = true
}
Po wywołaniu funkcji prepareForEditing
chcę animować ścieżkę prostokątną. Próbowałem
func prepareForEditing(editing:Bool){
UIView.animateWithDuration(0.5,
animations: {
self.rectanglePath = makeNewShape()
}
)
}
Nic się nie dzieje. Możesz mi powiedzieć, co jest nie tak z moim kodem? 8
Author: Dănuț Mihai Florian, 2014-11-10
2 answers
Aby animować CGPath nie można użyć UIView.metody animacji. Stworzyłem własną podklasę UIView, aby pokazać, jak można animować kształt ścieżek CGPaths, zapoznaj się z komentarzami i zmień je zgodnie z własnymi wymaganiami:
class MyView: UIView {
let shapeLayer = CAShapeLayer()
let maskLayer = CAShapeLayer()
var rectanglePath = UIBezierPath()
override func didMoveToSuperview() {
super.didMoveToSuperview()
backgroundColor = UIColor.clear
// initial shape of the view
rectanglePath = UIBezierPath(rect: bounds)
// Create initial shape of the view
shapeLayer.path = rectanglePath.cgPath
shapeLayer.strokeColor = UIColor.black.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
layer.addSublayer(shapeLayer)
//mask layer
maskLayer.path = shapeLayer.path
maskLayer.position = shapeLayer.position
layer.mask = maskLayer
}
func prepareForEditing(editing:Bool){
let animation = CABasicAnimation(keyPath: "path")
animation.duration = 2
// Your new shape here
animation.toValue = UIBezierPath(ovalIn: bounds).cgPath
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
// The next two line preserves the final shape of animation,
// if you remove it the shape will return to the original shape after the animation finished
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
shapeLayer.add(animation, forKey: nil)
maskLayer.add(animation, forKey: nil)
}
}
8
Author: Greg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-04 17:47:58
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-04 17:47:58
UIBezierPath do rysowania obramowania na UIButton za pomocą animacji typu loader
func animation(){
CATransaction.begin()
let layer : CAShapeLayer = CAShapeLayer()
layer.strokeColor = UIColor.purple.cgColor
layer.lineWidth = 3.0
layer.fillColor = UIColor.clear.cgColor
let path : UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: self.btn.frame.size.width + 2, height: self.btn.frame.size.height + 2), byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 5.0, height: 0.0))
layer.path = path.cgPath
let animation : CABasicAnimation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0.0
animation.toValue = 1.0
animation.duration = 7.0
CATransaction.setCompletionBlock{ [weak self] in
print("Animation completed")
}
layer.add(animation, forKey: "myStroke")
CATransaction.commit()
self.btn.layer.addSublayer(layer)
}
0
Author: Hardik Thakkar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-02 08:49:36
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-02 08:49:36