Flipping UIButton while changing image? - ios

I need to change an image of a UIButton ,where the user hit the button, it will be flipped, and change to a new image ,this one does not flips ,but only change the image .
CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:#"transform.rotation.y"];
crossFade.duration = 1.0;
crossFade.fromValue = (id)cell.imageView.image.CGImage;
crossFade.toValue = (id)Cellimg.CGImage;
crossFade.removedOnCompletion = NO;
crossFade.fillMode = kCAFillModeForwards;
[cell.imageView.layer addAnimation:crossFade forKey:#"rotationY"];
[cell setImage:Cellimg forState:UIControlStateNormal];
What am I doing wrong?

Why don't you create a custom button with an image view? Something like this-
UIImage *img = [UIImage imageNamed:#"path/to-image.png"];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.imageView.transform = CGAffineTransformMakeRotation(M_PI); // flip the image view
[button setImage:img forState:UIControlStateNormal]

Solved it with this :
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 0.50f];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:cell cache:NO];
// [baseBtn setTitle:newTitle forState:UIControlStateNormal];
[cell setImage:Cellimg forState:UIControlStateNormal];
[UIView commitAnimations];

Related

IOS Button touch up inside - First touch = Action1 and second touch = Action2// loop between the two

Hi all
I have attached below code. Upon launch, the view controller displays
a logo in the center and four main user buttons on the bottom.I try to
achieve that if the first button got touched up inside (Called Lists),
the logo fades and the center circle displays the name (Lists) of the
first button and shows around it buttons with different submenu
points, in the same time the 4 main (initial) buttons fade and are
only seen at a minimal (low Alpha). I want to achieve that if the
faded (half visible)main button gets touched again initial view is
restored with logo and only the main four menu buttons. Meaning, first
touch shows sub menus and its animations and second click returns to
initial view. Again clicked shows again submenu and so on. How can i
do that?
Thanks Dnai
//
// ViewController.h
// NanCom1
//
// Created by Daniel Habshush on 8/13/15.
// Copyright © 2015 Daniel Habshush. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (nonatomic, retain) IBOutlet UIImageView* logo;
#property (nonatomic, retain) IBOutlet UIButton* button1;
#property (nonatomic, retain) IBOutlet UIButton* button2;
#property (nonatomic, retain) IBOutlet UIButton* button3;
#property (nonatomic, retain) IBOutlet UIButton* button4;
#property (nonatomic, retain) IBOutlet UIButton* listMain;
#property (nonatomic, retain) IBOutlet UIButton* shoppingList;
#property (nonatomic, retain) IBOutlet UIButton* todoList;
#end
//
// ViewController.m
// NanCom1
//
// Created by Daniel Habshush on 8/13/15.
// Copyright © 2015 Daniel Habshush. All rights reserved.
//
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
{
UIDynamicAnimator* _animator;
UIGravityBehavior* _gravity;
UICollisionBehavior* _collision;
}
#synthesize logo, button1, button2, button3, button4, listMain, todoList, shoppingList;
- (void)viewDidLoad {
[super viewDidLoad];
logo = [[UIImageView alloc] init];
UIImage *logoPic = [UIImage imageNamed:#"Logo3.png"];
logo.image=logoPic;
logo.frame = CGRectMake(100, 200, 175, 175);
[logo setAlpha:0];
[self.view addSubview:logo];
[UIImageView beginAnimations:nil context:nil];
[UIImageView setAnimationDuration:3.0];
[UIImageView setAnimationDelay:0.5];
[UIImageView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.logo.alpha = 1.0;
[UIImageView commitAnimations];
button1 = [[UIButton alloc] init];
[button1 setFrame:CGRectMake(5, 2, 90, 90)];
[button1 setIsAccessibilityElement:YES];
button1.backgroundColor = [UIColor clearColor];
[button1 setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[button1 setTitle:#"Lists" forState:UIControlStateNormal];
[button1.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:13]];
[button1 addTarget:self action:#selector(pressed:) forControlEvents:UIControlEventTouchUpInside];
[button1 setEnabled:YES];
[self.view addSubview:button1];
button2 = [[UIButton alloc] init];
[button2 setFrame:CGRectMake(85, 2, 90, 90)];
[button2 setIsAccessibilityElement:YES];
button2.backgroundColor = [UIColor clearColor];
[button2 setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[button2 setTitle:#"Cirlce" forState:UIControlStateNormal];
[button2.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:13]];
[button2 setEnabled:YES];
[button2 addTarget:self action:#selector(pressed2:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
button3 = [[UIButton alloc] init];
[button3 setFrame:CGRectMake(160, 2, 90, 90)];
button3.backgroundColor = [UIColor clearColor];
[button3 setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[button3 setTitle:#"Location" forState:UIControlStateNormal];
[button3.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:13]];
[button3 setEnabled:YES];
[button2 addTarget:self action:#selector(pressed3:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3];
button4 = [[UIButton alloc] init];
[button4 setFrame:CGRectMake(250, 2, 90, 90)];
button4.backgroundColor = [UIColor clearColor];
[button4 setTitle:#"Menu" forState:UIControlStateNormal];
[button4.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:13]];
[button4 setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[button4 setEnabled:YES];
[button2 addTarget:self action:#selector(pressed4:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button4];
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
_gravity = [[UIGravityBehavior alloc] initWithItems:#[button1, button2, button3, button4]];
[_animator addBehavior:_gravity];
_collision = [[UICollisionBehavior alloc]
initWithItems:#[button1, button2, button3, button4]];
_collision.translatesReferenceBoundsIntoBoundary = YES;
[_animator addBehavior:_collision];
UIDynamicItemBehavior* itemBehaviour4 = [[UIDynamicItemBehavior alloc] initWithItems:#[button4]];
itemBehaviour4.elasticity = 0.4;
itemBehaviour4.resistance = 0.65;
[_animator addBehavior:itemBehaviour4];
UIDynamicItemBehavior* itemBehaviour3 = [[UIDynamicItemBehavior alloc] initWithItems:#[button3]];
itemBehaviour3.elasticity = 0.45;
itemBehaviour3.resistance = 0.5;
[_animator addBehavior:itemBehaviour3];
UIDynamicItemBehavior* itemBehaviour2 = [[UIDynamicItemBehavior alloc] initWithItems:#[button2]];
itemBehaviour2.elasticity = 0.5;
itemBehaviour2.resistance = 0.3;
[_animator addBehavior:itemBehaviour2];
UIDynamicItemBehavior* itemBehaviour1 = [[UIDynamicItemBehavior alloc] initWithItems:#[button1]];
itemBehaviour1.elasticity = 0.37;
itemBehaviour1.resistance = 0.2;
[_animator addBehavior:itemBehaviour1];
}
- (void)pressed:(UIButton*)button1 {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button1.alpha = 0.3;
self.button2.alpha = 0.3;
self.button3.alpha = 0.3;
self.button4.alpha = 0.3;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.logo.alpha = 0;
[UIView commitAnimations];
listMain = [[UIButton alloc] init];
[listMain setFrame:CGRectMake(100, 200, 175, 175)];
listMain.backgroundColor = [UIColor clearColor];
[listMain setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[listMain setTitle:#"Lists" forState:UIControlStateNormal];
[listMain.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:35]];
[listMain setAlpha:0];
[self.view addSubview:listMain];
todoList = [[UIButton alloc] init];
[todoList setFrame:CGRectMake(60, 145, 75, 75)];
todoList.backgroundColor = [UIColor clearColor];
[todoList setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[todoList setTitle:#"ToDo" forState:UIControlStateNormal];
[todoList.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:13]];
[todoList setAlpha:0];
[self.view addSubview:todoList];
shoppingList = [[UIButton alloc] init];
[shoppingList setFrame:CGRectMake(10, 245, 75, 75)];
shoppingList.backgroundColor = [UIColor clearColor];
[shoppingList setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[shoppingList setTitle:#"Shopping" forState:UIControlStateNormal];
[shoppingList.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:13]];
[shoppingList setAlpha:0];
[self.view addSubview:shoppingList];
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.listMain.alpha = 1;
self.todoList.alpha = 1;
self.shoppingList.alpha = 1;
[UIView commitAnimations];
}
- (void)pressed2:(UIButton*)button2 {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button2.alpha = 1;
self.button1.alpha = 1;
self.button3.alpha = 1;
self.button4.alpha = 1;
self.listMain.alpha = 0;
self.todoList.alpha = 0;
self.shoppingList.alpha = 0;
[UIView commitAnimations];
}
- (void)pressed3:(UIButton*)button3 {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.listMain.alpha = 0;
self.todoList.alpha = 0;
self.shoppingList.alpha = 0;
self.logo.alpha = 1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button2.alpha = 1;
self.button1.alpha = 1;
self.button3.alpha = 1;
self.button4.alpha = 1;
[UIView commitAnimations];
}
- (void)pressed4:(UIButton*)button4 {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button2.alpha = 1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button1.alpha = 1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button3.alpha = 1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button4.alpha = 1;
[UIView commitAnimations];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

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:

why is my UIButton changing size and position after assigning an image? How can I keep it the same?

my UIButton size and position changes after I assign an image to it. I am not sure why. Does anyone know?
I tried repositioning the button to its original position but it doesnt work.
CGRect buttonFrame = _subscribeButton.frame;
CGAffineTransform transform = _subscribeButton.transform;
[_subscribeButton setTitle:#"Subscribe" forState:UIControlStateNormal];
[_subscribeButton setImage:[UIImage imageNamed:#"bigButton.png"] forState:UIControlStateNormal];
[_subscribeButton setFrame:buttonFrame];
[_subscribeButton setTransform:transform];
I should be setting backgroundImage not image. this was the problem the whole time. aspect fill only effects the background image as this is scaled by the frame.
answer: [button setBackgroundImage:[UIImage imageNamed:#"blah.png"]];
try this
CGRect buttonFrame = _subscribeButton.frame;
CGAffineTransform transform = CGAffineTransformIdentity;
[_subscribeButton setTitle:#"Subscribe" forState:UIControlStateNormal];
[_subscribeButton setImage:[UIImage imageNamed:#"bigButton.png"] forState:UIControlStateNormal];
[_subscribeButton setFrame:buttonFrame];
[_subscribeButton setTransform:transform];
change this
CGAffineTransform transform = _subscribeButton.transform;
to
CGAffineTransform transform = CGAffineTransformIdentity;
Update:
CGRect buttonFrame = _subscribeButton.frame;
CGAffineTransform transform = CGAffineTransformIdentity;
[_subscribeButton setTitle:#"Subscribe" forState:UIControlStateNormal];
[_subscribeButton setImage:[UIImage imageNamed:#"bigButton.png"] forState:UIControlStateNormal];
[_subscribeButton setContentMode:UIViewContentModeScaleToFill];
_subscribeButton.autoresizingMask = UIViewAutoresizingNone;
_subscribeButton.autoresizesSubviews = NO;
[_subscribeButton setFrame:buttonFrame];
[_subscribeButton setTransform:transform];

UIButton animation ontarget self

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];
}

Info lightbutton animation

I am having trouble in showing infobutton to navigate to information page/view.
I have written this code.
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
toolBar.items = [NSArray arrayWithObjects:infoButtonItem, nil];
I got infolight button but i do not now know how to do animate & navigation action method in to flip to info page.
I also want infobutton to stay at center, currently it is at left corner.
This will create ur info button in center.
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[infoButton addTarget:self action:#selector(goToRechercherView) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *flexibleLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolbar.items = [NSArray arrayWithObjects:flexibleLeft,infoButtonItem,flexibleLeft, nil];
This is for swipe animation. add your view inside.
-(void)goToRechercherView{
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.view cache:YES];
[UIView commitAnimations];
}
There are two ways to do this...
First one is......
newView setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)]; //notice this is OFF screen!
[UIView beginAnimations:#"animateView" context:nil];
[UIView setAnimationDuration:0.4];
[newView setFrame:CGRectMake( 0.0f, 0.0f, 320.0f, 480.0f)]; //notice this is ON screen!
[UIView commitAnimations];
Second option is to use built in animations as flip and curl:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:newView
cache:YES];
[self.navigationController.view addSubview:settingsView.view];
[UIView commitAnimations];

Resources