Animated UIButton not registering press - ios

I am trying to make a slide out notification bar for when the app receives a notification and the app is currently open and in the foreground. I got this working with a custom UIView (actually it's a UIButton) and an animation. For some reason though it won't register a press when I tap on it. It won't call the selector method. Code is below:
-(void)appOpenPush {
//get string
NSLog(#"got a push while app was open");
AppDelegate *appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *pushString = appDelegate.pushString;
//play sound
AudioServicesPlaySystemSound(MessageSound);
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
//note I am actually using a button here
UIButton *pushView =[[UIButton alloc] initWithFrame:CGRectMake(0, -75, [[UIScreen mainScreen] bounds].size.width,75)];
pushView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
[pushView addTarget:self action:#selector(pushSlidePress) forControlEvents:UIControlEventTouchUpInside];
//set image
UIImageView *logo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon120.png"]];
logo.frame=CGRectMake(5, 20, 35, 35);
logo.layer.cornerRadius=8;
logo.clipsToBounds=YES;
[pushView addSubview:logo];
UILabel *pushLabel = [[UILabel alloc] initWithFrame:CGRectMake(48, 34, 0, 0)];
pushLabel.text=pushString;
pushLabel.font=[UIFont systemFontOfSize:13];
[pushLabel sizeToFit];
pushLabel.textColor=[UIColor whiteColor];
[pushView addSubview:pushLabel];
//animate
UIWindow* currentWindow = [UIApplication sharedApplication].keyWindow;
[currentWindow addSubview:pushView];
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
pushView.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width,75);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.4 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
pushView.frame = CGRectMake(0, -75, [[UIScreen mainScreen] bounds].size.width,75);
} completion:^(BOOL finished) {
[pushView removeFromSuperview];
}];
}];
}
Could anyone give me a pointer to why this isn't working? Is it because I am adding the UIButton to the main window? Any tips would be really helpful. thanks!

You have to add UIViewAnimationOptionAllowUserInteraction as an option to both of your animation blocks.
As per the documentation :
"Allow the user to interact with views while they are being animated."

Try this to avoid logo and pushLabel receive touches events:
logo.userInteractionEnabled = NO;
pushLabel.userInteractionEnabled = NO;

So it turns out you can't simply detect a press if the button is moving. I tested this by just adding the frame setup without it animating and it registered the press, when animation is turned on it doesn't. Seems that UIButton doesn't have tracking built in. I did come up with a quick fix. I just made a button the same size as the total animation area (i.e. the size of my slide out bar) and added that to the currentWindow on top of the animation frame. So it's just a clear invisible button on top of the animation but it's not moving itself. i.e:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,[[UIScreen mainScreen]bounds].size.width , 75)];
[button addTarget:self action:#selector(pushSlidePress) forControlEvents:UIControlEventTouchUpInside];
[currentWindow addSubview:button];
[UIView animateWithDuration:0.4 delay:0.0 options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{
pushView.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width,75);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.4 delay:2.0 options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{
pushView.frame = CGRectMake(0, -75, [[UIScreen mainScreen] bounds].size.width,75);
} completion:^(BOOL finished) {
[pushView removeFromSuperview];
[button removeFromSuperview];
}];
}];
Obviously you are covering an area of the screen that might block other things being pressed, but it depends on your setup. The animation is quick and I only have one button that the notification bar will block and it's too quick to notice. Hopefully this will be helpful to someone else trying to achieve a similar thing. Thanks for your help guys.

Related

Disable font animation in UIButton [duplicate]

This question already has answers here:
How to stop unwanted UIButton animation on title change?
(24 answers)
Closed 4 months ago.
I am animating a view that is sliding down. In the view there is a simple UIButton. The view and the button have fixed widths and heights (I checked with adding a color as background). Although when use:
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Slide Down the notification
CGRect notificationsRect = self.notificationContainerView.bounds;
notificationsRect.origin.y = 0;
notificationViewController.view.frame = notificationsRect;
} completion:^(BOOL finished) {
[notificationViewController didMoveToParentViewController:self];
self.currentNotification = notificationViewController;
}];
then the view slides down perfectly, expect for that the text in the UIButton kind of fades in. It starts really small and animates to the correct font size.
How can I disable the animation of the text in the UIButton?
Is the effect you have something similar to what is below? I tried to imitate your situation by having a view with a UIButton inside with some some text set for the title of the button. I then animated the button similarly to the way you did. As you can see there isn't really a fade happening on the text of my button inside my view that is sliding so I wonder if there is something else at play for you. I have the code I used below also to mock the situation.
- (void)viewDidLoad {
[super viewDidLoad];
self.myView = [[UIView alloc] initWithFrame:CGRectMake(200, 0, 100, 100)];
self.myView.backgroundColor = [UIColor greenColor];
UIButton *myButton= [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 80, 80)];
myButton.backgroundColor = [UIColor blueColor];
[myButton setTitle:#"fun times" forState:UIControlStateNormal];
[self.myView addSubview:myButton];
[self.view addSubview:self.myView];
UIButton *resetButton = [[UIButton alloc] initWithFrame:CGRectMake(300, 300, 50, 50)];
resetButton.backgroundColor = [UIColor blackColor];
[resetButton addTarget:self action:#selector(reset) forControlEvents:UIControlEventTouchUpInside];
UIButton *goButton = [[UIButton alloc] initWithFrame:CGRectMake(300, 100, 50, 50)];
goButton.backgroundColor = [UIColor redColor];
[goButton addTarget:self action:#selector(go) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goButton];
[self.view addSubview:resetButton];
}
- (void)reset {
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Slide Up the notification
CGRect notificationsRect = self.myView.bounds;
notificationsRect.origin.y = 0;
notificationsRect.origin.x = 200;
self.myView.frame = notificationsRect;
} completion:nil];
}
- (void)go {
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Slide Down the notification
CGRect notificationsRect = self.myView.bounds;
notificationsRect.origin.y = 200;
notificationsRect.origin.x = 200;
self.myView.frame = notificationsRect;
} completion:nil];
}
Disable animations for UIButton using setAnimationsEnabled:(BOOL)enabled
Reference:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/clm/UIView/setAnimationsEnabled:

Show banner from top of the screen

I want to create a turn-based match. When player received a notification player:receivedTurnEventForMatch:didBecomeActive:
I want to show a banner that slides from the top of the screen. "Your turn. Play" similar to the banner that Game Center shows when player is authenticated.
How can I do it? Should it be a UIViewController or UIAlert, or what?
here is a good example for notification for what you want, i hope that help you:
https://github.com/ekurutepe/MPNotificationView
or
https://github.com/edgurgel/CMNavBarNotificationView
or
https://github.com/terryworona/TWMessageBarManager
There are MANY ways to do what you mentioned. This is just one of them. Feel free to change the code around and read up on UIView animations to you can try out different things yourself.
-(void)myMethod {
// create UILabel and set its properties
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 320, 50)];
myLabel.backgroundColor = [UIColor grayColor];
NSString *myLabelText = #"Welcome back, Mike Lyman"; // showing you to use NSString for label text
myLabel.textAlignment = NSTextAlignmentCenter;
myLabel.text = myLabelText;
[self.view addSubview:myLabel]; // add UILabel to view
myLabel.frame = CGRectMake(0, -50, 320, 50); // set text label frame offscreen
// start the animation to slide UILabel into visible view
[UIView animateWithDuration:0.5 delay:0
options:UIViewAnimationOptionCurveLinear
animations:^ {
myLabel.frame = CGRectMake(0, 20, 320, 50);
}
completion:^(BOOL finished) {
// after 1 second delay, start sliding UILabel out of visible view
[UIView animateWithDuration:0.5 delay:1
options:UIViewAnimationOptionCurveLinear
animations:^ {
myLabel.frame = CGRectMake(0, -50, 320, 50);
}
completion:^(BOOL finished) {
NSLog(#"Done");
[self.view removeFromSuperview]; // remove UILabel from view completely
}];
}];
}

Creating a custom view with an animation

Im trying to make a custom view where one of it's elements(a UILabel) has animation that when a button of the custom view is tapped the label should expand and then contract. The first part of the animation happens correctly but not the second. Im using this method.
[UIView animateWithDuration:1.3 animations:^{
CGRect frame = CGRectMake(50, 15, 140, 20);
[labelFav setFrame:frame];
} completion:^(BOOL finished) {
//[labelFav setHidden:YES];
[UIView animateWithDuration:1.3 animations:^{
CGRect frame = CGRectMake(50, 15, 0, 20);
[labelFav setFrame:frame];
}];
}];
I believe it's a problem with how the methods Refresh and LayoutSubviews are implemented.
I did it following this tutorial, ray wenderlinch
The problem comes when after the first animation is called, LayoutSubviews is called, so all the items are set to the initial position, and when the first animation is finished the second one starts but the items are set to the original state, so I believe the animation doesnt happen because there's been a change between animations.
I would like to know how to implement this correctly, can you lend me a hand?
Thanks.
PS: This is a second question about the same problem but with a different angle.my previous question
[UPDATE]
This is my code for LayoutSubviews:
- (void)layoutSubviews {
[super layoutSubviews];
if(_idEvent == 0) return;
_buttonFav = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[_buttonFav setImage:[UIImage imageNamed:#"fav.png"] forState:UIControlStateNormal];
[_buttonFav addTarget:self action:#selector(pressedFavButton:) forControlEvents:UIControlEventTouchDown];
_labelButton = [[UILabel alloc]initWithFrame:CGRectMake(0, 45, 0, 20)];
_labelButton.text = #"LABEL";
[self addSubview:_labelButton];
[self addSubview:_buttonFav];
}
Which is called right before the second animation. My refresh method is:
- (void)refresh {
if(selected){
[_buttonFav setImage:[UIImage imageNamed:#"fav_sel.png"] forState:UIControlStateNormal];
}
else{
[_buttonFav setImage:[UIImage imageNamed:#"fav.png"] forState:UIControlStateNormal];
}
}
Where I just set the image for the button depending on an attribute that lets me know if the button was tapped.
I do, how to make an animation with Auto-layout then? With the new
XCode you dont have an a option to disable it.
Add a width constraint to a label. Then create IBOutlet to this constraint in your implementation file (see the section 3. here, if you don't know how to do that), let's say something like:
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttonWidthConstraint;
Then you can animate it this way:
[UIView animateWithDuration:1.3 animations:^{
self.buttonWidthConstraint.constant = 140;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.3 animations:^{
self.buttonWidthConstraint.constant = 0;
[self.view layoutIfNeeded];
}];
}];
use SetTransform and CGAffineTransform
try this out
[UIView animateWithDuration:1.3 animations:^{
[labelFav setTransform:CGAffineTransformMakeScale(0.5,0.5)];
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.3 animations:^{
[labelFav setTransform:CGAffineTransformMakeScale(1.0,1.0)];
}];
}];
Try to encapsulate the UILabel into a UIView and animate that view.
UIView *auxView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
[auxView addSubview:labelFav];
[UIView animateWithDuration:1.3 animations:^{
CGRect frame = CGRectMake(50, 15, 140, 20);
[auxView setFrame:frame];
} completion:^(BOOL finished) {
//[labelFav setHidden:YES];
[UIView animateWithDuration:1.3 animations:^{
CGRect frame = CGRectMake(50, 15, 10, 20);
[auxView setFrame:frame];
}];
}];

How to fit image in UITextView and Tap to get larger

I have created a Table-view in main ViewController once i touch the row it will take to the next view controller(DetailViewController) where it display the image and name of dish.And once i tap the image it should get larger but it is not getting bigger .The below code is which i have tried in DetailViewController.
DetailViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
imageView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10,100, 100)];
CGRect textViewFrame = CGRectMake(10, 10, 300, 400);
textView = [[UITextView alloc] initWithFrame:textViewFrame];
textView.returnKeyType = UIReturnKeyDone;
textView.backgroundColor=[UIColor whiteColor];
textView.editable=NO;
textView.delegate = self;
[self.view addSubview:textView];
[textView addSubview:imageView];
textView.alpha = 0.9;
textView.font = [UIFont systemFontOfSize:15];
if(mrowno==0)
{
imageView.image=[UIImage imageNamed:#"Dosa.jpg"];
textView.text = #"\n\n\n\n\n\n\n\nDOSA\nDosa, ";
imageButton = [[UIButton alloc]init];
[imageButton setFrame:CGRectMake(0, 0, 100, 100)];
[imageButton addTarget:self action:#selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:imageButton];
}
Here is the method to make image larger
-(void)imageTapped:(UIButton*)in_sender
{
[UIView transitionWithView:in_sender.superview duration:0.8f options:UIViewAnimationOptionCurveLinear animations:^
{
[in_sender.superview setFrame:CGRectMake(0, 0, 300, 500)];
}
completion:^(BOOL finished)
{
}];
}
You can increase and decrease the size of image by pinch in and pinch out,If it required
-(void)viewDidLoad
{
UIPinchGestureRecognizer* pinch=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinching:)];
[self.imageView addGestureRecognizer:pinch];
}
-(void)pinching:(UIGestureRecognizer*) recognizer
{
UIPinchGestureRecognizer* pinch=(UIPinchGestureRecognizer*) recognizer;
self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
pinch.scale=1;
NSLog(#"pinching");
}
You do not want to use transitionWithView. Instead, you want to use + animateWithDuration: delay:options:animations:completion: (or any of its shorter variants...)
[UIView animateWithDuration:0.3f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
viewYouWantToAnimate.frame = CGRectMake(..., ..., ..., ...);
} completion:^{
//Add completion code here, or pass nil if you don't have anything to do.
}];
Edit: In case you were wondering what transitionWithView was used for... the AppleDocs have a great example of it adding animations for remove/addSubview methods (not frame modifications):
[UIView transitionWithView:containerView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[fromView removeFromSuperview];
[containerView addSubview:toView];
} completion:NULL];

iOS - Closing a Subclassed UIView from view's uibutton

I have a custom subclassed uiview I created to display in-app notifications.
I can call the view just fine, but am having problems dismissing it using a uibutton (embedded in the custom view)
When the button is pressed, the app crashes and I get this error:
UPDATE - Fixed the above issue, but now only the button dismisses, and not the actual view. See updated code below.
-(id)initWithMessage:(NSString *)message{
self = [super initWithFrame:CGRectMake(0, -70, 320, 60)];
if (self) {
//Add Image
UIImage *image = [UIImage imageNamed:#"notice-drop-down"];
UIImageView *background = [[UIImageView alloc] initWithImage:image];
[self addSubview:background];
//Add Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.frame.size.height/2-25, 300, 50)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor blackColor]];
[label setText:message];
label.numberOfLines = 0;
[label setFont:[UIFont fontWithName:#"Hand of Sean" size:16]];
//NSLog(#"FONT FAMILIES\n%#",[UIFont familyNames]);
[self addSubview:label];
//Add Close Button
UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(280, self.frame.size.height/2-15, 30, 30)];
UIImage *closeImage = [UIImage imageNamed:#"notice-close"];
[closeButton setImage:closeImage forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(closeNoticeDropDown:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:closeButton];
//Animate In
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationCurveEaseIn
animations:^{
self.frame = CGRectMake(0,70,320,60);
}
completion:nil
];
}
return self;
}
-(void)closeNoticeDropDown:(id)self{
NoticeDropDown *notice = (NoticeDropDown *)self;
NSLog(#"Frame: %f",notice.frame.size.width);
//Animate In
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationCurveEaseOut
animations:^{
notice.frame = CGRectMake(0,-70,320,60);
}
completion:^(BOOL finished){
[notice removeFromSuperview];
//notice = nil;
}
];
}
View call from another view controller:
noticeDropDown = [[NoticeDropDown alloc] initWithMessage:message];
[self.view insertSubview:noticeDropDown belowSubview:hudContainerTop];
You have declared your method to be a class method but are sending the message to an instance. If you want it to be a class method still, pass [NoticeDropDown class] as the target parameter to your addTarget:action:forControlEvemts: method. Otherwise replace the "+" with a "-" in the method declaration.
Also- when UIControl actions have sender parameters it will send the control as the sender - so you will get a UIButton instead of your view.
My recommendation is to change your action to an instance method and replace "sender" with "self".
I apologize for formatting I'm posting from my phone. I'll try to fix when back at a computer.
EDIT:
Change your updated method like so:
-(void)closeNoticeDropDown:(id)sender{
NSLog(#"Frame: %f",notice.frame.size.width);
//Animate In
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationCurveEaseOut
animations:^{
self.frame = CGRectMake(0,-70,320,60);
}
completion:^(BOOL finished){
[self removeFromSuperview];
}
];
}
You don't need to pass self into a method, it always exists.
You probably try to call your method on your view instance (something like [noticeDropDown closeNoticeDropDown:...]) but closeNoticeDropDown: is a class method and you should call it this way:
[NoticeDropDown closeNoticeDropDown: noticeDropDown];
There's also few things in your animation code that look wrong:
[UIView commitAnimations]; calls should be removed as they are used in pair with [UIView beginAnimations:... context:...] method, they're not required with block-based animation
[sender removeFromSuperview]; call should go in animation's completion block, otherwise it will be called before your animation is even started and you won't get desired effect

Resources