UIButton touchUp get swallowed - uikit

touching a UIButton rapidly does not deliver all touches up
to the receiver.
Is there any recourse?
iOS 12 in case that matters. both real device and simulator

I was animating touches in the handler for that touch up:
// https://stackoverflow.com/questions/46021640/how-to-sequence-two-animations-with-delay-in-between
let scaleForwardAnimationDuration: TimeInterval = 0.15
let transformBackAnimationDuration: TimeInterval = 0.1
let animationDuration: TimeInterval = scaleForwardAnimationDuration + transformBackAnimationDuration
UIView.animateKeyframes(withDuration: animationDuration, delay: 0, options: [], animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: scaleForwardAnimationDuration) {
sender.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
}
UIView.addKeyframe(withRelativeStartTime: scaleForwardAnimationDuration, relativeDuration: transformBackAnimationDuration) {
sender.transform = .identity
}
})
and that caused input dropped on the floor
due to .allowUserInteraction
absent from options of the outer animation block

Related

run and jump and return to running SpriteKit Swift

I'm new to programming with SpriteKit and Swift. I have followed a few tutorials, but I'm now stucking for a couple of days.
i have multiple animations in an separate Actions file, "idle", "run" and jump. When my games start you see the idle figure and when you tap it will move from idle animation to the running animation. So far so good. But when my figure start jumping with next tap, the running animation is also still on my screen. When the jumps finish the jump animation is gone. How can I remove the running animation for a second?
So far the only other option I've got is to remove also the running animation as the jump animation. They won't comeback.
This is my hero file where if have the actions for running and jumping:
func runPlayer(){
let playerRun:SKAction = SKAction(named: "run", duration: moveSpeed)!
let runAction:SKAction = SKAction.moveBy(x: 0, y: 0, duration: moveSpeed)
let runGroup:SKAction = SKAction.group([playerRun, runAction])
thePlayerRuns.run(runGroup, withKey: "run")
thePlayerRuns.physicsBody?.isDynamic = false
thePlayerRuns.position = CGPoint(x: 0 , y: 0)
thePlayerRuns.size = CGSize(width: 160.25, height: 135.55)
thePlayerRuns.zPosition = 99
addChild(thePlayerRuns)
}
func jumpPlayer(){
let playerJump: SKAction = SKAction(named: "jump", duration: moveSpeed)!
let jumpUp: SKAction = SKAction.moveBy(x: 0, y: 50, duration: 0.5)
let jumpDown: SKAction = SKAction.moveBy(x: 0, y: -80, duration: 0.5)
let jumpSeq: SKAction = SKAction.sequence([jumpUp, jumpDown])
let jumpGroup: SKAction = SKAction.group([playerJump, jumpSeq])
thePlayerJumps.run(jumpGroup, withKey: "jump")
thePlayerJumps.physicsBody?.affectedByGravity = true
thePlayerJumps.physicsBody?.allowsRotation = false
thePlayerJumps.physicsBody?.isDynamic = true
thePlayerJumps.position = CGPoint(x: 0 , y: 30)
thePlayerJumps.size = CGSize(width: 160.25, height: 135.55)
thePlayerJumps.zPosition = 99
addChild(thePlayerJumps)
thePlayerRuns.removeFromParent()
thePlayerJumps.run(
SKAction.sequence([SKAction.wait(forDuration: 1.0),SKAction.removeFromParent()])
)
// thePlayerRuns.run(
// SKAction.sequence([SKAction.removeFromParent()])
//
// )
let otherWait = SKAction.wait(forDuration: 1)
let otherSequence = SKAction.sequence([otherWait, SKAction(runPlayer())])
run(otherSequence)
/*
let otherWait = SKAction.waitForDuration(1)
let otherSequence = SKAction.sequence([otherWait, SKAction.repeatActionForever(sequence)])
runAction(otherSequence)
*/
}
And this is my Gamescene:
hero = MTHero()
hero.position = CGPoint(x: 70, y: 278)
addChild(hero)
hero.idlePlayer()
}
func start(){
isStarted = true
hero.stop()
movingGround.start()
hero.runPlayer()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if !isStarted {
start()
}else{
hero.jumpPlayer()
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
I thing I have to do something in thouchesBagan with another else statement.
Thanks in advance! Moi
I've managed to solve it. I'm not sure that it is the best option. But in my jumpPlayer function I have added the following code:
self.run(SKAction.wait(forDuration: 1)) {
self.addChild(self.thePlayerRuns)
just under
thePlayerJumps.run(
SKAction.sequence([SKAction.wait(forDuration: 1.0),SKAction.removeFromParent()])
)
If you have suggestion for a better solution, please let me know.

Swift 3 how to animate font in a button

I have a button that gets bigger and smaller with an animation. How can I make the font get bigger and smaller along with the button. My code is below.
func animate() {
let originalFrame = self.playButton.frame
let originalBackgroundColor = self.playButton.backgroundColor
UIView.animateKeyframes(withDuration: 2, delay: 0, options: UIViewKeyframeAnimationOptions.calculationModeLinear, animations: {
// make the button grow and become dark gray
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.5) {
self.playButton.frame.size.height = self.playButton.frame.size.height * 1.2
self.playButton.frame.size.width = self.playButton.frame.size.width * 1.2
self.playButton.frame.origin.x = self.playButton.frame.origin.x - (self.playButton.frame.size.width / 12)
self.playButton.frame.origin.y = self.playButton.frame.origin.y - (self.playButton.frame.size.height / 12)
}
// restore the button to original size and color
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
self.playButton.frame = originalFrame
self.playButton.backgroundColor = originalBackgroundColor
}
}, completion: nil)
}
You can store the grab the original font size and scale it similar to how you do it with the frame and background color:
func animate() {
// Original frame and background color
let originalFontSize = self.playButton.titleLabel?.font.pointSize
// First UIView.addKeyframe()
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.5) {
self.playButton.titleLabel?.font = UIFont.systemFont(ofSize: originalFontSize!*1.2)
// Other animations ...
}
// Second UIView.addKeyframe() to return to normal
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
self.playButton.titleLabel?.font = UIFont.systemFont(ofSize: originalFontSize!)
// Other animations ...
}
Look into animating the transform property instead.
Something like
self.playButton.transform = CGAffineTransform.init(scaleX: 1.2, y: 1.2)

Swift Infinite Fade in and Out Loop

How can I make a effect in swift similar to this:
I want the animation to loop forever.
For iOS
UIViewAnimationOptions set provides different handy options to achieve a combination of beautiful and complex animations. For your particular scenario you will require two of the options.
UIViewAnimationOptions.Repeat
UIViewAnimationOptions.AutoReverse
Check out the code below for implementation.
Code:
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
view.backgroundColor = UIColor.blueColor()
self.view.addSubview(view)
UIView.animateWithDuration(1,
delay: 0,
options: [UIViewAnimationOptions.Autoreverse, UIViewAnimationOptions.Repeat],
animations: {
view.backgroundColor = UIColor.clearColor()
},
completion: nil)
}
}
Explanation:
I have created a view with a specific frame for demo purpose.
The part you are interested in is the UIView.animateWithDuration method. Notice that I have provided an array [UIViewAnimationOptions.AutoReverse, UIViewAnimationOptions.Repeat] in the options parameter.
These two options are enough to achieve a smooth and forever looping animation like below.
https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/6Iow7n7WiWf6Naz/autoReverse.gif
If you don't want to reverse the animation, just remove UIViewAnimationOptions.AutoReverse from the array in the options parameter. You will get an animation like this.
https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/8fyRUlzqNHSQI47/noreverse.gif
For iOS
let viewSquare be the name of the blue square in your question.
UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat,.autoreverse], animations: {
viewSquare.alpha = 0.0
}, completion: nil)
Swift 5.1
let duration = 0.5
func fadeIn(finished: Bool) {
UIView.animate(withDuration: self.duration, delay: 0,
options: [.curveEaseInOut],
animations: { self.tftMap.alpha = 1 }, completion: self.fadeOut)
}
func fadeOut(finished: Bool) {
UIView.animate(withDuration: self.duration, delay: 0,
options: [.curveEaseInOut],
animations: { self.tftMap.alpha = 0 }, completion: self.fadeIn)
}
I assume you are programming for iOS.
Play around with the duration to see what suits you best:
class ViewController: UIViewController {
#IBOutlet weak var myView: UIView!
let duration = 0.5
override func viewDidLoad() {
super.viewDidLoad()
self.fadeOut(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func fadeIn(finished: Bool) {
UIView.animateWithDuration(self.duration, delay: 0, options: [.CurveEaseInOut], animations: { self.myView.alpha = 1 } , completion: self.fadeOut)
}
func fadeOut(finished: Bool) {
UIView.animateWithDuration(self.duration, delay: 0, options: [.CurveEaseInOut], animations: { self.myView.alpha = 0 } , completion: self.fadeIn)
}
}
Swift 5
This worked well for me. I wanted to animate a continuous fade in and out of a label, which I placed inside the "cardHeaderView" UIView.
#IBOutlet weak var cardHeaderView: UIView!
Place this inside the "viewDidAppear". I went with a delay of zero so the animation would start right away.
fadeViewInThenOut(view: cardHeaderView, delay: 0)
Here is the function.
func fadeViewInThenOut(view : UIView, delay: TimeInterval) {
let animationDuration = 1.5
UIView.animate(withDuration: animationDuration, delay: delay, options: [UIView.AnimationOptions.autoreverse, UIView.AnimationOptions.repeat], animations: {
view.alpha = 0
}, completion: nil)
}
Swift 5
Fade in 2 seconds
pause 2 seconds,
fade out 2 seconds
Repeat
func startAnimation() {
UIView.animateKeyframes(withDuration: 6.0,
delay: 0,
options: [.repeat, .autoreverse, .calculationModeLinear]) {
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.165) { [weak self] in
self?.view1.alpha = 0.0
}
UIView.addKeyframe(withRelativeStartTime: 0.165, relativeDuration: 0.165) { [weak self] in
self?.view2.alpha = 1.0
}
UIView.addKeyframe(withRelativeStartTime: 0.66, relativeDuration: 0.165) { [weak self] in
self?.view2.alpha = 0.0
}
ctartTime: 0.825, relativeDuration: 0.165) { [weak self] in
self?.view1.alpha = 1.0
}
}
}
Please note that when your animation is at Alpha == 0.0 the item is not interactable! You will have to add to .allowUserInteraction as an option
If you want repeatable fade animation you can do that by using CABasicAnimation like below :
###First create handy UIView extension :
extension UIView {
enum AnimationKeyPath: String {
case opacity = "opacity"
}
func flash(animation: AnimationKeyPath ,withDuration duration: TimeInterval = 0.5, repeatCount: Float = 5){
let flash = CABasicAnimation(keyPath: AnimationKeyPath.opacity.rawValue)
flash.duration = duration
flash.fromValue = 1 // alpha
flash.toValue = 0 // alpha
flash.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
flash.autoreverses = true
flash.repeatCount = repeatCount
layer.add(flash, forKey: nil)
}
}
###How to use it:
// You can use it with all kind of UIViews e.g. UIButton, UILabel, UIImage, UIImageView, ...
imageView.flash(animation: .opacity, withDuration: 1, repeatCount: 5)
titleLabel.flash(animation: .opacity, withDuration: 1, repeatCount: 5)

UIView Animations seem to speed up after first time, Xcode 6.1 swift

For some reason after my first call of a serious of animation blocks, the animation seems to be faster, not sure if this is a bug or something i've done wrong but i'm sure someone call tell me.. i've made a UIView subclass to handle this.
import UIKit
import QuartzCore
class GBPopupController: UIView {
var originalContainerCenterY = CGFloat()
#IBOutlet var continerConstraintCenterY: NSLayoutConstraint!
#IBOutlet var containerConstraintCenterX: NSLayoutConstraint!
var startingCenter = CGPoint()
#IBOutlet var contentView: UIView!
#IBOutlet var button: UIButton!
override func layoutSubviews() {
super.layoutSubviews()
contentView.layer.cornerRadius = 10
button.layer.cornerRadius = 10
}
override func didMoveToSuperview() {
self.beginViewAnimations()
}
func animatePopupIn() {
UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.7, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.contentView.layer.transform = CATransform3DIdentity
}, completion: {finished in
})
}
func beginViewAnimations() {
var transform = CATransform3DIdentity;
transform = CATransform3DMakeTranslation(0, -self.frame.size.height, 0)
self.contentView.layer.transform = transform
UIView.animateWithDuration(0.4, delay: 0.0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
self.backgroundColor = UIColor(white: 0.0, alpha: 0.5)
}, completion: {finished in
self.animatePopupIn()
})
}
func removeViewFromSuperView() {
UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
var transform = CATransform3DIdentity;
transform = CATransform3DMakeTranslation(0, -self.frame.size.height, 0)
self.contentView.layer.transform = transform
}, completion: {finished in
UIView.animateWithDuration(0.4, delay: 0.0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
self.backgroundColor = UIColor.clearColor()
}, completion: {finished in
self.removeViewAnimation()
})
})
}
func removeViewAnimation() {
self.removeFromSuperview()
}
My comment is too long so I answer here. I don't know if it can help you but here's what I did.
I use autolayout and so in the viewdidload(), dimensions are not yet initialized. They are based on a 600x600 size screen (meaning any width, any height). Screen size is picked up once the first action is done (like pushing a button in my case).
This is why I call my animation for the first time at this first action, just to put my frame at the good place. Then the rest is the same. But it kind of initialize my view with the right frame (size and position). After that, I don't have this problem and my animation is also good.
It's like I was calling it twice the first time, one to initialize and one just to play the animation.
I made some research and I found that it could be a problem with the layer which is different from the frame and which is initialized with a different animation from the one you choose. That's why I didn't have the same animation the first time.
Hope it will help you.

Swift Playground XCPShowView "Unable to satisfy constraints"

I'm trying to get a Live View to show a simple animation in a Swift Playground. Whenever I import the XCPlayground framework to execute the XCPShowView function i get this error:
Playground execution failed: error: Couldn't lookup symbols:_CGPointMake
The error changes for a few other "symbols" as well, including CGRectMake.
After being advised to modifying my code to remove the "make" from methods such as CGRectMake I still get an error from Xcode when I try to animate my view. The error message is really long, but basically it says
"Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want."
Here is the code I am trying to work with:
//Circle Button
class timerButtonGraphics: UIView {
override func drawRect(rect: CGRect) {
let colorGreen = UIColor(red: 0.310, green: 0.725, blue: 0.624, alpha: 1.000)
let colorRed = UIColor(red: 241/255, green: 93/255, blue: 79/255, alpha: 100)
var bounds = self.bounds
var center = CGPoint()
center.x = bounds.origin.x + bounds.size.width / 2
center.y = bounds.origin.y + bounds.size.height / 2
var radius = 61
var path:UIBezierPath = UIBezierPath()
path.addArcWithCenter(center, radius: CGFloat(radius), startAngle: CGFloat(0.0), endAngle: CGFloat(Float(M_PI) * 2.0), clockwise: true)
path.strokeWithBlendMode(kCGBlendModeNormal, alpha: 100)
path.lineWidth = 2
if strokeRed == true {
colorRed.setStroke()
}
else {
colorGreen.setStroke()
}
path.stroke()
}
var strokeRed = true
}
var test = timerButtonGraphics(frame: CGRect(x: 0, y: 400, width: 400, height: 400))
UIView.animateWithDuration(2.0, delay: 2, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: nil, animations: {
test.transform = CGAffineTransformMakeTranslation(0.5, 0)
}, completion: nil)
XCPShowView("Circle Animation", test)
try with this, maybe it can help you
test.setTranslatesAutoresizingMaskIntoConstraints(false)

Resources