UIButton animation ontarget self - ios

I am working on a app with two UIButtons which have to change size and color when they are pressed. My code is:
UIButton *Answer1Button = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *Answer2Button = [UIButton buttonWithType:UIButtonTypeCustom];
[Answer1Button addTarget:self action:#selector(Answer1Action) forControlEvents:UIControlEventTouchUpInside];
[Answer2Button addTarget:self action:#selector(Answer2Action) forControlEvents:UIControlEventTouchUpInside];
[Answer1Button setBackgroundColor:[UIColor redColor]];
[Answer2Button setBackgroundColor:[UIColor yellowColor]];
Answer1Button.frame = CGRectMake(0, 0, 320, 45);
Answer2Button.frame = CGRectMake(0, 50, 320, 45);
And the function I created for it:
-(void)Answer1Action{
[UIButton beginAnimations:nil context:nil];
[UIButton setAnimationDuration:0.5];
self.backgroundColor = [UIColor greenColor];
self.alpha = 0.5;
[UIButton commitAnimations];
}
The problem I encounter right now, is when I press the UIButton the function is called but the self.alpha affects the entire UIView in which the button is located in.
I'm still a rookie with Objective-C so I'm thinking it is probably something simple I forgot.

[Answer1Button setBackgroundColor:[UIColor greenColor]];
[Answer1Button setAlpha:0.5];
//Include QuartzCore framework and import it to your viewController.h,and write the below line along with the above code to change the size and to do many animations.
Answer1Button.layer.affineTransform=CGAffineTransformScale(Answer1Button.transform, 1.2, 1.2);

You are setting self.alpha = 0.5;, here self is not your button. You should set your button:
Answer1Button.backgroundColor = [UIColor greenColor];
Answer1Button.alpha = 0.5;
But this is not the best practice. You can send your button to the handler method as param:
(Variable and method names should not start with capital letter in ObjC.)
[answer1Button addTarget:self action:#selector(answer1Action:) forControlEvents:UIControlEventTouchUpInside];
And use the sender button as parameter in your handler method:
-(void)answer1Action:(id)sender{
UIButton* myButton = (UIButton*)sender;
[UIButton beginAnimations:nil context:nil];
[UIButton setAnimationDuration:0.5];
myButton.backgroundColor = [UIColor greenColor];
myButton.alpha = 0.5;
[UIButton commitAnimations];
}

Set button alpha instead of self.alpha=0.5;
-(void)Answer1Action{
[UIButton beginAnimations:nil context:nil];
[UIButton setAnimationDuration:0.5];
self.backgroundColor = [UIColor greenColor];
Answer1Button.alpha = 0.5;
[UIButton commitAnimations];
}

Related

Scroll view button action is not working when animating

Added 10 UIButtons in a UIScrollView. Wrote automatically scroll the scrollview code using UIAnimation.
for (int i=0; i<10; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(XPosition, YPosition, width,50);
[button setTitle:#"Warning animation items array " forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
if (index%2) {
button.backgroundColor = [UIColor blueColor];
} else{
button.backgroundColor = [UIColor purpleColor];
}
XPosition = paddingSpace + XPosition + button.frame.size.width;
[_scroll addSubview:button];
index++;
}
_scroll.contentSize=CGSizeMake(XPosition,50);
When animating the UIScrollView, I can't able tap any UIButton!!!
[UIView animateWithDuration:20
delay:0.0
options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat)
animations:^{
_scroll.contentOffset = CGPointMake(XPosition, 0.0);
} completion:^(BOOL finished){}];
If the animation is stop only I can tap the UIButton.
Please help me out this.
you can use scrollview.userintreactionenable=yes;
i think this help for you

Issues in scroll view

Actually There are four buttons and there is sub buttons for each button.once I click first button the second button have to move down by giving space for sub buttons but its not working even after setting scroll view.
bgsroll=[[UIScrollView alloc]init];
bgsroll.frame=CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
bgsroll.userInteractionEnabled=YES;
bgsroll.scrollEnabled=YES;
// bgsroll.backgroundColor=[UIColor grayColor];
bgsroll.showsVerticalScrollIndicator=YES;
bgsroll.contentSize=CGSizeMake(200, 2000);
[self.view addSubview:bgsroll];
happyBtn=[[UIButton alloc]init];
happyBtn.frame=CGRectMake(MainView.frame.size.width*0.12, MainView.frame.size.height*0.1, CGRectGetWidth(self.view.frame)/1.5, 40);
[happyBtn setTitle:#"Happiness" forState:UIControlStateNormal];
[happyBtn setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
[happyBtn addTarget:self action:#selector(tapHappy:) forControlEvents:UIControlEventTouchUpInside];
[bgsroll addSubview:happyBtn];
happyView=[[UIView alloc]init];
happyView.frame=CGRectMake(MainView.frame.size.width*0.26,CGRectGetHeight(happyBtn.frame)*2.2, CGRectGetWidth(MainView.frame)/1.6, CGRectGetHeight(MainView.frame)/3);
happyView.layer.cornerRadius=8.0;
happyView.layer.borderWidth=0.5;
happyView.layer.borderColor=[[UIColor blueColor]CGColor];
[bgsroll addSubview:happyView];
LettingGo=[[UIButton alloc]init];
LettingGo.frame=CGRectMake(happyView.frame.size.width*0.01,happyView.frame.size.height*0.1, CGRectGetWidth(happyView.frame)/1.05, 50);
[LettingGo setTitle:#"Letting go of negativity" forState:UIControlStateNormal];
LettingGo.titleLabel.numberOfLines=2;
LettingGo.titleLabel.adjustsFontSizeToFitWidth=YES;
[LettingGo setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
[happyView addSubview:LettingGo];
LivingPresent=[[UIButton alloc]init];
LivingPresent.frame=CGRectMake(happyView.frame.size.width*0.01,LettingGo.frame.size.height*1.4, CGRectGetWidth(happyView.frame)/1.05, 40);
[LivingPresent setTitle:#"Living in the present" forState:UIControlStateNormal];
LivingPresent.titleLabel.adjustsFontSizeToFitWidth=YES;
[LivingPresent setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
[happyView addSubview:LivingPresent];
happyView.hidden=YES;
CreateBtn=[[UIButton alloc]init];
CreateBtn.frame=CGRectMake(MainView.frame.size.width*0.12, MainView.frame.size.height*0.5, CGRectGetWidth(self.view.frame)/1.5, 40);
[CreateBtn setTitle:#"Happiness" forState:UIControlStateNormal];
[CreateBtn setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
[CreateBtn addTarget:self action:#selector(tapHappy:) forControlEvents:UIControlEventTouchUpInside];
[bgsroll addSubview:CreateBtn];
-(void)tapHappy:(id)selector{
happyView.hidden=!happyView.hidden;
}
By not working you mean it is not scrolling when the buttons are pushed? I find it easier to scroll elements creating a method that changes the coordinates of elements in subviews through animations. Something like this:
- (void)viewDidLoad {
expandButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[expandButton1 addTarget:self action:#selector(expandButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)expandButtonPressed:(id)sender{
if (expanded == NO) {
expanded = YES;
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionRepeat animations:^{
[UIView setAnimationRepeatCount:1];
image1.frame = CGRectMake(image1.frame.origin.x, image1.frame.origin.y+200.0f, image1.frame.size.width, image1.frame.size.height);
image.frame2 = CGRectMake(image2.frame.origin.x, image2.frame.origin.y+200.0f, image2.frame.size.width, image2.frame.size.height);
expandButton2.frame = CGRectMake(expandButton2.frame.origin.x, expandButton2.frame.origin.y+200.0f, expandButton2.frame.size.width, expandButton2.frame.size.height);
expandButton3.frame = CGRectMake(expandButton3.frame.origin.x, expandButton3.frame.origin.y+200.0f, expandButton3.frame.size.width, expandButton3.frame.size.height);
}completion:nil];
}

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 a UIViewController as Alert View in iOS

I'm trying to create a custom Alert View similar to this
I want to use a View Controller as the alert because the 'X' button on the top left corner must be there, and neither a regular UIAlertController or any pod I found can help me with that.
How can I achieve this?
Thank you.
For showing viewController as UIAlertView try this
SecondViewController *secondViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"SecondViewController"];
secondViewController.view.frame = self.view.frame;
[self.view addSubview:secondViewController.view];
secondViewController.view.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
secondViewController.view.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
}];
try this..
#interface alertViewController ()
{
UILabel *labelTitle;
UIView *viewToShow,*viewAlert;
}
#end
#implementation alertViewController
- (void)viewDidLoad
{
[super viewDidLoad];
viewToShow = [[UIView alloc]initWithFrame:CGRectMake(0,0, 165*2, 400)];
[viewToShow setBackgroundColor:[UIColor whiteColor]];
[viewToShow setOpaque:NO];
[self.navigationController.view addSubview:viewToShow];
labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 165*2, 30)];
[[labelTitle layer] setCornerRadius:14];
labelTitle.text=#"TITLE";
[labelTitle setTextAlignment:NSTextAlignmentCenter];
[viewToShow addSubview:labelTitle];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(cancelButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"X" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:#"helvetica-neue" size:20]];
button.frame = CGRectMake(5,5,30, 40.0);
[viewToShow addSubview:button];
UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(10, 60,viewToShow.frame.size.width-20, 250)];
[textView setBackgroundColor:[UIColor lightGrayColor]];
[viewToShow addSubview:textView];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self
action:#selector(cancelButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:#"Continue" forState:UIControlStateNormal];
button1.frame = CGRectMake((viewToShow.frame.size.width/2)-40,viewToShow.frame.size.height-40,80,40);
[viewToShow addSubview:button1];
[[viewToShow layer] setCornerRadius:14];
CGPoint centerForCustomExportView;
if (UIDeviceOrientationIsLandscape((UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation]))
{
centerForCustomExportView = CGPointMake(([[UIScreen mainScreen]bounds].size.height>[[UIScreen mainScreen]bounds].size.width?[[UIScreen mainScreen]bounds].size.height:[[UIScreen mainScreen]bounds].size.width)/2, (([[UIScreen mainScreen]bounds].size.height>[[UIScreen mainScreen]bounds].size.width?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2));
}
else
{
centerForCustomExportView = CGPointMake([[UIScreen mainScreen]bounds].size.width/2, ([[UIScreen mainScreen]bounds].size.height-self.navigationController.navigationBar.frame.size.height)/2);
}
viewToShow.center = centerForCustomExportView;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceRotated:)name:UIDeviceOrientationDidChangeNotification object:nil];
}
try this.....
- (void)show
{
NSLog(#"show");
isShown = YES;
self.transform = CGAffineTransformMakeScale(0.1, 0.1);
self.alpha = 0;
[UIView beginAnimations:#"showAlert" context:nil];
[UIView setAnimationDelegate:self];
self.transform = CGAffineTransformMakeScale(1.1, 1.1);
self.alpha = 1;
[UIView commitAnimations];
}

iOS Subview with buttons not responding to touches.

I have a UIView Subclass that has a frame/image with buttons in it. I'm trying to add it to my view controller however when I do it shows up but none of the buttons will register ANY touches.
Here is my UIView Subclass.
#import "ControlView.h"
#implementation ControlView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGRect slideFrame = CGRectMake(0, 402, 320, 82);
slider = [[UIView alloc] initWithFrame:slideFrame];
slider.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"shelf.png"]];
[self addSubview:slider];
UIImage *btnImage = [UIImage imageNamed:#"NavBar_01.png"];
UIImage *btnImageSelected = [UIImage imageNamed:#"NavBar_01_s.png"];
btnImage = [UIImage imageNamed:#"NavBar_01.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_01_s.png"];
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(12, 28, 70, 50);
[btn1 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn1];
[slider bringSubviewToFront:btn1];
[btn1 addTarget:self action:#selector(home) forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_02.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_02_s.png"];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(87, 28, 70, 50);
[btn2 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn2 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn2];
[slider bringSubviewToFront:btn2];
// [btn2 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_03.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_03_s.png"];
btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
btn3.frame = CGRectMake(162, 28, 70, 50);
[btn3 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn3 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn3];
[slider bringSubviewToFront:btn3];
// [btn3 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_04.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_04_s.png"];
btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
btn4.frame = CGRectMake(237, 28, 70, 50);
[btn4 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn4 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn4];
[slider bringSubviewToFront:btn4];
// [btn4 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(slideUp)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(slideDown)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[self addGestureRecognizer:recognizer];
}
return self;
}
-(void)slideUp
{
// Slide up based on y axis
CGRect frame = slider.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
frame.origin.y = 320;
slider.frame = frame;
[UIView commitAnimations];
}
-(void)slideDown
{
CGRect frame = slider.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
frame.origin.y = 425;
slider.frame = frame;
[UIView commitAnimations];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
And this is how I'm adding it to my ViewController.
ControlView *cont = [[ControlView alloc]init];
[homeView.view addSubview:cont];
I tried adding the same code to a ViewController and the buttons etc.. worked so I'm sure this is something simple/stupid with Delegates/Self etc.. between the subview and the ViewController however I have a mental block.
You are doing this?
ControlView *cont = [[ControlView alloc]init];
you are supposed to do
ControlView *cont = [[ControlView alloc]initWithFrame:self.view.frame];

Resources