animating button Allowuserinteraction not working
Take out self.nextButton.alpha = 0
to test this out, I believe that button events will not fire when alpha is zero. When it comes to animations, the actual object's alpha will get set to zero before the animation starts, and what you are seeing is like a rendered interactive screenshot of your view as it animates
If that is the case, then you need to set the alpha to something like 0.0100000003 or override the button to be interactive at alpha 0
UIView.animate with allowUserInteraction enables the UIButton only in the animation's final frame
Doesn't matter that it's a UIButton. Whether it's a button or an imageview, the only way to tap it where it is showing during the animation seems to be to use touchesBegan and test whether the object's layer.presentation frame contains the touch location:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
let touchLocation = touch.location(in: self.view)
let buttonFrame = theButton.layer.presentation()!.frame
if buttonFrame.contains(touchLocation) {
print("Tapped the button here!")
}
}
UIButton can't be touched while animated with UIView animateWithDuration
The touchable part of the button will not coincide with the button's visible frame when it is being animated.
Internally, the button's frame will be set to the final value from your animation. You should find that you can tap in this area, and it would work.
To hit a moving button, you need to do hit testing on the button's .layer.presentationLayer
property (need to import the QuartzCore framework to do this). This would typically be done in touch handling methods in your view controller.
I am happy to expand on this answer if you need more.
Here is how you would respond to a touch event by hit testing presentation layers. This code is in the view controller subclass that is managing your buttons. When I did this I was interested in the initial touch rather than the touch ending (which is typically when a tap would register) but the principle is the same.
Remember to import the QuartzCore framework and add it to your project.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
for (UIButton *button in self.buttonsOutletCollection)
{
if ([button.layer.presentationLayer hitTest:touchLocation])
{
// This button was hit whilst moving - do something with it here
break;
}
}
}
UIButtons are not responding during animation - iOS Swift 3
Call different method of UIView:
func myButton () {
sender.layer.backgroundColor = firstColor
sender.setTitleColor(UIColor.white, for: UIControlState.normal)
UIView.animate(withDuration: 0.5,
delay: 0.0,
options: .allowUserInteraction,
animations: {
sender.layer.backgroundColor = secondColor },
completion: nil)
}
Having problems allowing interaction in UIView animation
Are you setting your button alpha to 0?
If yes here is an interesting thing about animation.
What you see on the screen during the animation is not what the application sees.
The moment you set your alpha to 0, the alpha is 0 for that view, even if you are still seeing it on the screen.
Also, a view that has an alpha lower that 0.05 (don't recall the exact number) won't get touch event.
What you can do is to implement the - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
of that view's superview. or the touchesEnded...
as you like.
(Assuming that your not setting it's alpha to 0.)
So you can test for touche that occur where the button is, or just remove that button and let any touch on the screen cancel your animation.
You may also be interested in this post:
Core Animation, unexpected animated position and hitTest values
Animating Button .Repeat make @IBAction Not working
I had to add .allowUserInteraction
to the options parameter as noted by @Shripada
UIView.animateWithDuration(0.5, delay: 0.4, options: [.repeat, .autoreverse, .allowUserInteraction], animations: {
self.button.frame = CGRect(x: 80, y: 80, width: 50, height: 50)
}, completion: nil)
UIButton not interacting during animation
There were some questions here recently about this. The animation is purely visual. You can tap the button in it'd old frame until the animation is complete, when it finishes, the actual button jumps.
EDIT:
This answer is what I was referring to. You need to manually move the button with an NSTimer, apparently. See the linked question/answer for more.
Other folks suggest passing UIViewAnimationOptionAllowUserInteraction
as a UIViewAnimation option.
UIView Animation and UIButton stop working b/c Home pressed - Swift 4
First of all, if you have a multiple animations within the same ViewController
(VC) that occur after playButton
is pressed, then that may explain why the it is becoming disabled upon return from background. Why? I don't know. But I had a similar issue and resolved it by creating a new class
and VC for the multiple animations that were originally set to occur when my UIButton
was pressed. Inside of my button's IBAction
, I simply created a segue to then new VC.
With regards to the animation, you could approach this two ways: 1) Pause and Resume the animation using CALayer OR 2) Simply use NotificationCenter without even having to touch any AppDelegate code. I prefer simple ways b/c it will save time and effort. So, try this code in the VC where the button animation is to occur:
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector:#selector(goingToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector:#selector(openingApp), name: UIApplication.willEnterForegroundNotification, object: nil)
UIView.animate(withDuration: 1.0,
delay: 0,
options: [.autoreverse, .repeat, .allowUserInteraction],
animations: {
self.playButton.transform = CGAffineTransform(scaleX: 1.175, y: 1.175)
},
completion: nil)
}
@objc func goingToBackground(){
playButton.transform = .identity
}
@objc func openingApp(){
self.view.layoutIfNeeded()
UIView.animate(withDuration: 1.0,
delay: 0.3,
options: [.autoreverse, .repeat, .allowUserInteraction],
animations: {
self.playButton.transform = CGAffineTransform(scaleX: 1.175, y: 1.175)}, completion: nil)
self.view.layoutIfNeeded()
}
Related Topics
Ruby: Loaderror - Library Not Found for Class Digest::Sha1 -- Digest/Sha1
Uibezierpath Subclass Initializer
How to Work with Udp Sockets in iOS, Swift
Screen Recording When My iOS App Is in Background with Replaykit
Delete Image from Photo Gallery
Scaling Physics Bodies in Xcode Spritekit
How to Make Async/Await in Swift
Longer Subtitles in Mapview Annotations (Swift)
How to Make App Wait Until Firebase Request Is Finished
Uitextfield Starting Cursor Position Is Wrong
Pure Swiftui Login, Signup, Register Flow, Is It Possible
Swift 3 - Passing Data Between a View Controller and After That to Another 2
iOS Uiimagepickercontroller: Any Way of Getting the Date of the Chosen Picture
Mirror Not Working in Swift When Iterating Through Children of an Objective-C Object
Checkbox Uitableview with Different Sections
Random Number from an Array Without Repeating the Same Number Twice in a Row