Button not preforming action from being tapped while animating in Xcode using UIView Animation - ios

I have a game that I am making and I am trying to move a button named tapMe using UIView Animation but I need it to be tapeable while it is being animated.
[UIView animateWithDuration:0.5 animations:^{
_tapMe.center = CGPointMake(xRandom, yRandom);
}];
Where xRandom and yRandom are randomly generated integers and _tapMe is connected to the button, I am certain because it does move.

When you use animateWithDuration, the button's frame is moved to its final position immediately (it will be touchable there), but the presentation layer is animated, so where you see the button during the animation is not where the touchable area will be. You either need to hit test the presentation layer, or animate the button by moving it in small increments with a timer. See the answer here for more detail, UIButton can't be touched while animated with UIView animateWithDuration

Related

UIView animateWithDuration not reacting as I expected

I'm a little confused by UIView animateWithDuration.
I have a label placed at (224,93,152,39), centred horizontally in my storyboard. When I run viewDidLoad, I want it to slide up from centre vertically and horizontally to the position I've placed in my storyboard. So intuitively, I did this:
[UIView animateWithDuration:1.0 animations:^{
self.titleLabel.frame = CGRectMake(self.titleLabel.frame.origin.x,self.view.center.y, 0, 0);
}];
This actually did the opposite for me. The label slides from above the status bar to the desired position (224,93,152,39). I want it to start from centre of the viewController to the desired position (224,93,152,39).
I think I'm not getting something essential of the UIView animations. Would appreciate it if someone can point it out for me.
May be you can try by place the above code to viewDidAppear method
The frame you set in the animations block is the ending frame. Set the starting frame right before you call animateWithDuration if needed.

Can't touch during ""Moving"" animation (Block Animation)

I have a View with UIButton, UITextField, and a UIImageView for Background.
in viewDidLoad i try animate UIImageView from alpha=0 to alpha =1 using block. it's pretty basic, here's the code:
[UIView animateWithDuration:20.0 options:UIViewAnimationOptionAllowUserInteraction
animations:^{
self.movingImg.frame = CGRectMake(button.frame.origin.x,button.frame.origin.y, self.movingImg.frame.size.width, self.movingImg.frame.size.height);
}
completion:nil];
which is working fine. but during that 1.5 seconds of animation, my touch in current view seems to be disabled. i can't click on the image that is moving (animation is according to position).
Thanks in Advance
Set the alpha level to 0.01. That is the lowest Alpha according to the Apple Documentation which allows touch to work.
See Does UIButton become disabled when its alpha is set to 0.0?

How to translate an entire UIView and retain gesture recognition?

I have a UIView "MainView" that initially appears as follows:
The gradient bar is part of MainView, the whitespace beneath is part of a Container View subview.
When the search button in top-right is tapped, I animate a searchBar from offscreen to be visible in the view:
I manage to do this by the following code:
CGRect currentViewFrame = self.view.bounds;
currentViewFrame.origin.y += searchViewHeight;
[UIView animateWithDuration:0.4
delay:0.0
usingSpringWithDamping:1.0
initialSpringVelocity:4.0
options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.view.frame = currentViewFrame;
}
completion:^(BOOL finished)
{
}];
Visually, the result of this animation is perfect. The entire view shifts, and the searchBar is now on screen. However, the searchBar does not respond to interaction. Correct me if I'm wrong, but I expect this is because the MainView's frame no longer includes the screen area that the searchBar now occupies, so its effectively a gesture deadzone.
So this makes me think that instead of lazily animating the entire MainView down to accomodate the searchBar, I must instead individually translate all subviews of MainView one at a time. In this simple situation, that would not be a big problem, but I can envision a circumstance with tens of subviews making that completely unrealistic.
What is the best method to accomplish what I am trying to do? Is there a secret to animating entire views/subviews without having gesture deadzones? Thanks in advance!!

fade in/fade out UISegmentedControl in XIB

I have a UISegmentedControl set up in my XIB. I want it to appear on viewDidLoad and if the user taps the area of the screen it's in, and then to disappear if the user taps it again or to fade out if the user leaves it alone.
In looking around for how to manage this I've found a lot of stuff about fading UIViews, but not as much on fading individual subviews, and little at all on fading elements in the XIB. I tried to adapt the UIView stuff but failed.
How can I make this work?
EDIT: Okay, I've got the appearance at viewDidLoad and the fade out working. But when the user taps the area where the UISegmentedControl is (now invisible because alpha=0), nothing happens. This is the code I'm using:
- (IBAction)tapInvisibleSegContr
//This is connected to the UISegmentedControl with the action Touch Up Inside. Until now, the segmented control has been at alpha=0 since fading after viewDidLoad.
{
self.segContrAsOutlet.alpha=1.0;
[self fadeMethodThatWorksInViewDidLoad];
NSLog(#"Yup, tapped.");
}
I'm not even getting the NSLog. I've got the action hooked up to the UISegmentedControl, with the action Touch Up Inside. What am I missing?
If it is resident in a xib, just put his alpha to 0, do the properly connections: an Outlet and an IBAction for value changed
Then in the viwDidLoad right after [super viewDidLoad] write:
[UIView animateWithDuration:1 animations:^{self.mySegOutlet.alpha = 1;}];
Inside the IBAction right after you code the answer before the last } write:
[UIView animateWithDuration:1 animations:^{self.mySegOutlet.alpha = 0;}];
This is the easiest method.
Bye
In the xib set your control's alpha to 0.0, then use UIView animation methods to animate its alpha to 1.0. For example:
[UIView animateWithDuration:1.0 animations:^{
self.segmentedControl.alpha = 1.0f;
}];
EDIT: To your problem with not getting the action called, try attaching it for the value changed control event - I don't think UISegmentedControl sends for touch up inside.

UIView alpha = 0 causes touches to be dropped to the view below

So I created a glass pane or a custom UIView to handle touches. This glass pane sits on top of other views such as dummy UIButtons. When I set the alpha to be 0, the touches actually get intercepted by the views underneath the glass view. This is wrong. However, when I set the alpha to a low value like 0.2, the glass pane intercepts the touches.
The alpha setting was done in Interface Builder.
Anybody know how to set to the alpha to 0 and still get have this glass pane intercept touches?
Yes, it is standard behaviour.
For example, you can just set clear background of that UIView:
UIView *touchHandlerView;
touchHandlerView.backgroundColor = [UIColor clearColor];
In such case user wouldn't see that view - I assume that you want to do exactly that?
Yes, alpha=0.0f; and hidden=YES; have the same effect. This is very useful, because you can animate a fade-out and not have to worry about touches on the invisible object - ie you can say:
-(void)hideOverlayButton
{
[UIView beginAnimations:#"hideOverlayButton" context:nil];
[UIView setAnimationDuration:0.2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[self.overlayButton setAlpha:0.0];
[UIVIew commitAnimations];
}
Then, when the animation is complete, the button won't respond to touches - there's no need to worry about deactivating it.
If what you want is to have invisible buttons, put a UIButton on the view, make sure it's at the front (bottom of the list in interface builder), set it's type to custom; don't set an image, background image or title. By default in interface builder you'll get alpha of 1 and a clear color.Then you can wire-up the IBOutlets in the usual way.

Resources