gpt4 book ai didi

ios - 圆形到矩形变换动画

转载 作者:搜寻专家 更新时间:2023-11-01 05:46:42 26 4
gpt4 key购买 nike

我是 iOS 的新手,我需要做以下动画:

enter image description here

圆形到矩形的转换应该是平滑的,但在上面的动画中它不是很平滑。

我所做的是使用以下代码创建一个圆形和一个矩形 in this tutorial :

  Circle : 
class OvalLayer: CAShapeLayer {

let animationDuration: CFTimeInterval = 0.3

override init() {
super.init()
fillColor = Colors.red.CGColor
path = ovalPathSmall.CGPath
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

var ovalPathSmall: UIBezierPath {
return UIBezierPath(ovalInRect: CGRect(x: 50.0, y: 50.0, width: 0.0, height: 0.0))
}

var ovalPathLarge: UIBezierPath {
return UIBezierPath(ovalInRect: CGRect(x: 2.5, y: 17.5, width: 95.0, height: 95.0))
}

var ovalPathSquishVertical: UIBezierPath {
return UIBezierPath(ovalInRect: CGRect(x: 2.5, y: 20.0, width: 95.0, height: 90.0))
}

var ovalPathSquishHorizontal: UIBezierPath {
return UIBezierPath(ovalInRect: CGRect(x: 5.0, y: 20.0, width: 90.0, height: 90.0))
}

func expand() {
let expandAnimation: CABasicAnimation = CABasicAnimation(keyPath: "path")
expandAnimation.fromValue = ovalPathLarge.CGPath// change ovalPathLarge to ovalPathSmail for animation
expandAnimation.toValue = ovalPathLarge.CGPath
expandAnimation.duration = animationDuration
expandAnimation.fillMode = kCAFillModeForwards
expandAnimation.removedOnCompletion = false
addAnimation(expandAnimation, forKey: nil)
}

}

Rectangle :

class RectangleLayer: CAShapeLayer {


override init() {
super.init()
fillColor = Colors.clear.CGColor
lineWidth = 5.0
path = rectanglePathFull.CGPath
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

var rectanglePathFull: UIBezierPath {
let rectanglePath = UIBezierPath()
rectanglePath.moveToPoint(CGPoint(x: 0.0, y: 100.0))
rectanglePath.addLineToPoint(CGPoint(x: 0.0, y: -lineWidth))
rectanglePath.addLineToPoint(CGPoint(x: 100.0, y: -lineWidth))
rectanglePath.addLineToPoint(CGPoint(x: 100.0, y: 100.0))
rectanglePath.addLineToPoint(CGPoint(x: -lineWidth / 2, y: 100.0))
rectanglePath.closePath()

// fillColor = Colors.red.CGColor
return rectanglePath
}

// var topLeft: UIBezierPath {}

func animateStrokeWithColor(color: UIColor, view : UIView) {
strokeColor = color.CGColor

// CATransaction.setDisableActions(true)
// view.layer.bounds.size.height = view.layer.bounds.width + 50

let strokeAnimation: CABasicAnimation = CABasicAnimation(keyPath: "bounds.size.width") //bounds.size.width
strokeAnimation.fromValue = view.layer.bounds.width
strokeAnimation.toValue = view.layer.bounds.size.width - 50
strokeAnimation.duration = 0.4
addAnimation(strokeAnimation, forKey: nil)
}
}

my view :

protocol HolderViewDelegate:class {
func animateLabel()
}

class HolderView: UIView {

let ovalLayer = OvalLayer()
let redRectangleLayer = RectangleLayer()

var parentFrame :CGRect = CGRectZero
weak var delegate:HolderViewDelegate?

override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = Colors.clear
}

required init?(coder: NSCoder) {
super.init(coder: coder)
}

func addOval() {
layer.addSublayer(ovalLayer)
ovalLayer.expand()
// NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: "wobbleOval",
// userInfo: nil, repeats: false)
}

func expandRectangle(){
NSTimer.scheduledTimerWithTimeInterval(0.45, target: self,
selector: "drawRedAnimatedRectangle",
userInfo: nil, repeats: false)
}

func drawRedAnimatedRectangle() {
layer.addSublayer(redRectangleLayer)
redRectangleLayer.animateStrokeWithColor(Colors.red,view: self)
}

但是我不知道怎么做我的动画,谁能帮帮我吗?

最佳答案

如果您希望同时放大和缩小角半径,您可以简化 the code from my other answer显着

您现在不再需要将动画“链接”在一起,因此您可以将它们添加到单个 CAAnimationGroup 并同时运行它们。

除了添加了 groupAnim 属性和删除了 cornerRadiusUndoAnim 之外,我们使用的属性将保持几乎相同。

class ViewController2: UIViewController {

let animLayer = CALayer() // the layer that is going to be animated
let cornerRadiusAnim = CABasicAnimation(keyPath: "cornerRadius") // the corner radius reducing animation
let widthAnim = CABasicAnimation(keyPath: "bounds.size.width") // the width animation
let groupAnim = CAAnimationGroup() // the combination of the corner and width animation
let animDuration = NSTimeInterval(1.0) // the duration of one 'segment' of the animation
let layerSize = CGFloat(100) // the width & height of the layer (when it's a square)

...

我们现在可以只添加 CAAnimationGroup 的设置,添加我们的圆角半径动画和缩放动画

override func viewDidLoad() {
super.viewDidLoad()

let rect = view.frame

animLayer.backgroundColor = UIColor.blueColor().CGColor // color of the layer, feel free to change
animLayer.frame = CGRect(x: rect.width-layerSize*0.5, y: rect.height-layerSize*0.5, width: layerSize, height: layerSize)
animLayer.cornerRadius = layerSize*0.5;
animLayer.anchorPoint = CGPoint(x: 1, y: 1) // sets so that when the width is changed, it goes to the left
view.layer.addSublayer(animLayer)

// decreases the corner radius
cornerRadiusAnim.duration = animDuration
cornerRadiusAnim.fromValue = animLayer.cornerRadius
cornerRadiusAnim.toValue = 0;
cornerRadiusAnim.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) // timing function to make it look nice

// increases the width
widthAnim.duration = animDuration
widthAnim.fromValue = animLayer.frame.size.width
widthAnim.toValue = rect.size.width
widthAnim.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) // timing function to make it look nice

// adds both animations to a group animation
groupAnim.animations = [cornerRadiusAnim, widthAnim]
groupAnim.duration = animDuration;
groupAnim.autoreverses = true; // auto-reverses the animation once completed

}

最后,我们可以在触摸 View 时运行组动画,两个动画将同时运行(完成后自动反转)。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
animLayer.addAnimation(groupAnim, forKey: "anims") // runs both animations concurrently
}

结果

enter image description here


完整项目:https://github.com/hamishknight/Circle-to-Rect-Animation

关于ios - 圆形到矩形变换动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35032410/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com