Reputation: 21
I am new to swift and this is my first question ever .... I would like to shrink a ball with duration 2 seconds, and then grow it for a duration of 5 seconds. My problem is that the second duration is ignored (ball shrinks for 2 seconds and grows for 2 seconds). I hope someone can help me.
This is my attempt:
let ball = UIView()
ball.frame = CGRectMake(50, 50, 50, 50)
ball.backgroundColor = UIColor.blueColor()
ball.layer.cornerRadius=25
relaxContainer.addSubview(ball)
UIView.animateWithDuration(2.0, delay:0, options: [.Repeat, .Autoreverse], animations: {
ball.frame = CGRectMake(50, 50, 20, 20)
}, completion: { finished in
UIView.animateWithDuration(5.0, animations: {
ball.frame = CGRectMake(50, 50, 50, 50)
})
})
Upvotes: 1
Views: 3948
Reputation: 21
My Answer thanks to help by Matt (duration times, variables from original question were changed):
Swift 2
let duration = 6.0
let delay = 0.0
UIView.animateKeyframesWithDuration(duration, delay: delay, options: [.Repeat], animations: {
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1/3, animations: {
ball.frame = CGRectMake(screenWidth/8*3, screenHeight/8*3, screenWidth/4, screenWidth/4)
})
UIView.addKeyframeWithRelativeStartTime(1/3, relativeDuration: 2/3, animations: {
ball.frame = CGRectMake(screenWidth/4, screenHeight/4, screenWidth/2, screenWidth/2)
})
}, completion: nil
)
Swift 3, 4, 5
let duration = 6.0
let delay = 0.0
UIView.animateKeyframes(withDuration: duration, delay: delay, options: [.repeat], animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1/3, animations: {
ball.frame = CGRect(x: screenWidth/8*3, y: screenHeight/8*3, width: screenWidth/4, height: screenWidth/4)
})
UIView.addKeyframe(withRelativeStartTime: 1/3, relativeDuration: 2/3, animations: {
ball.frame = CGRect(x: screenWidth/4, y: screenHeight/4, width: screenWidth/2, height: screenWidth/2)
})
}, completion: nil
)
Upvotes: 1