CGAffineTransformMakeScale animation on a cornerRadius rounded UIButton - ios

I am rounding a UIBUtton, which is fine (self is a uibutton subclass):
self.layer.cornerRadius = self.frame.size.width/2;
self.layer.masksToBounds = YES;
self.clipsToBounds = YES;
But I am also trying to animate the button to shrink the scale then return to original size, like so:
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(0.0f, 0.0f);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(1.0f, 1.0f);
} completion:nil];
}];
The shrink/restore animation happens as it should. However, i lose the corner radius afterwards and the button goes back to a square. How can I animate and change the transform scale while maintaining the circular button?
I have also tried the following. It restores the button back to circular state at the end of the animation, but there is a fraction of a second where it boxes back to a square at the very beginning of the animation and it looks pretty bad:
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(0.0f, 0.0f);
self.layer.cornerRadius = 0.0f;
self.layer.masksToBounds = YES;
self.clipsToBounds = YES;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(1.0f, 1.0f);
self.layer.cornerRadius = self.frame.size.width/2;
self.layer.masksToBounds = YES;
self.clipsToBounds = YES;
} completion:nil];
}];
EDIT: I need the animation code and the rounding the button code, to all happen within the subclassed button. It needs to happen internally from the button class when the button is touched (ie, no code for rounding the button or calling the animation can be in the view controller).
EDIT WITH CLARIFIED SOLUTION:
I have no idea why this worked, but a portion of the below suggestion seemed to fix the problem. I was adding the animation method as an action on the custom button for UIControlEventTouchDown. I removed the control event action and instead called the animation method from -(void)touchesBegan.... inside the subclassed button class and everything seems to be working fine. Not sure why this happened. Would love further clarification/explanation, in a comment or something, if someone knows why this worked. Thanks again to #Shan for the help

Try by putting the animation part in controller class not in subclassed button class.
//in customButton.h file
#import <UIKit/UIKit.h>
#interface customButton : UIButton
- (id)initWithFrameOfCustomButton:(CGRect)frame;
- (void)animate;//this method u need to add and call it from the controller
#end
//in the subclassed UIButtin class
//in customButton.m file
#import "customButton.h"
#import <QuartzCore/QuartzCore.h>
#implementation customButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithFrameOfCustomButton:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
//hear only set the frame only not incude animation part
//this is the custom initiliser that initilises the custom button with the given frame and also make it to round
self.frame = frame;
self.backgroundColor = [UIColor greenColor];
self.layer.cornerRadius = frame.size.width/2;
self.layer.masksToBounds = YES;
}
return self;
}
//add the animation part
- (void)animate //this method is called within the this class
{
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(0.0f, 0.0f);
//edit: comment below lines becz u already made it to round
// self.layer.cornerRadius = 0.0f;
// self.layer.masksToBounds = YES;
// self.clipsToBounds = YES;
// CATransform3D t = CATransform3DIdentity;
// t = CATransform3DMakeScale(0 , 0, 1.0f);
// cButton.layer.transform = t;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(1.0f, 1.0f);
// edit comment below lines
// self.layer.cornerRadius = self.frame.size.width/2;
// self.layer.masksToBounds = YES;
// self.clipsToBounds = YES;
// CATransform3D t = CATransform3DIdentity;
// t = CATransform3DMakeScale(1 , 1, 1.0f);
// cButton.layer.transform = t;
} completion:nil];
}];
}
//implement touch handling methods to call animation
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self animate];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self animate];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// [self animate];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// [self animate];
}
in the controller class include the animation part like below
//in the view controller that u are using this custom button
#import "FirstViewController.h"
#import "CustomCell.h"
#import "customButton.h"
#import <QuartzCore/QuartzCore.h>
#interface FirstViewController ()
#end
#implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
customButton *cButton = [[customButton alloc]initWithFrameOfCustomButton:CGRectMake(20, 30, 100, 100)]; //hear i am initilising the custom button
cButton.tag = 100;//assigning tag to access during the animation
[self.view addSubview:cButton];
// [self makeAnimation];//i am animation rite after it loads the all views u can place this method call where ever u want
//edit: i am commenting the this line so animations of the button in this class won't call
}
//edit below method is added
- (void)viewDidAppear:(BOOL)animated
{
customButton *cButton = (customButton *)[self.view viewWithTag:100];
//edit->comment this line so that u never call the animation from view controller
// [cButton animate];//animating the button the code in the subclassed method is called
}
- (void)makeAnimation
{
customButton *cButton = (customButton *)[self.view viewWithTag:100];//get the custom button by using the tag
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
cButton.layer.affineTransform = CGAffineTransformMakeScale(0.0f, 0.0f);
// cButton.layer.cornerRadius = 0.0f;
// cButton.layer.masksToBounds = YES;
// cButton.clipsToBounds = YES;
// CATransform3D t = CATransform3DIdentity;
// t = CATransform3DMakeScale(0 , 0, 1.0f);
// cButton.layer.transform = t;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
cButton.layer.affineTransform = CGAffineTransformMakeScale(1.0f, 1.0f);
// cButton.layer.cornerRadius = cButton.frame.size.width/2;
// cButton.layer.masksToBounds = YES;
// cButton.clipsToBounds = YES;
// CATransform3D t = CATransform3DIdentity;
// t = CATransform3DMakeScale(1 , 1, 1.0f);
// cButton.layer.transform = t;
} completion:nil];
}];
}
note UIButton inherits from: UIControl -> UIView -> UIResponder -> NSObject
see docs if u are confused
if you want use UIControlers i.e responder then u need to implement these methods
you can get the event for which u want,
in your subclassed UIButton class implement this by commenting touch handling methods
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
return YES;
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
return YES;
}

Similar to Shankar BS answer, but with swift 3.
Transformation with bounced animation:
myButton.layer.setAffineTransform(CGAffineTransform(scaleX: 0.0, y: 0.0))
UIView.animate(withDuration: 0.7, delay: 1.0,
usingSpringWithDamping: 0.7, initialSpringVelocity: 7.0, options: [],
animations: {
myButton.layer.setAffineTransform(CGAffineTransform(scaleX: 1.0, y: 1.0))
},
completion: nil
)

Related

Create A Shadow(or blur) when user Click on UIbutton?

I have a One Uibutton in Buttom. when User click on that button a uiview animation is display like popup from buttom to up.
this is my code
-(void)viewDidLoad
{
[super viewDidLoad];
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
}
- (IBAction)animated:(id)sender
{
if(_isAnimated)
{
[UIView animateWithDuration:0.3
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 520, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
}
completion:^(BOOL finished){
}];
_isAnimated=NO;
}
else
{
[UIView animateWithDuration:0.3
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
}
completion:^(BOOL finished)
{
}];
_isAnimated=YES;
}
}
Uiview animation is perfectly working but when uiview appear than background view is create shadow(or blur)like uiactionsheet and after finished animation background view is clear.
i dont know how to implemented.
plz help me
i am new in ios...
You can add a UIView in background and set its backgroundcolor black and alpha 0.5 in storyboard. Then change its alpha from 0.5 to 0 with animation. You can also add an UITapGestureRecognizer to detect taps on it and dissmiss the view when user taps outside.
-(void)viewDidLoad
{
[super viewDidLoad];
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
}
- (IBAction)animated:(id)sender
{
if(_isAnimated)
{
[UIView animateWithDuration:0.3
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 520, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
//showing background view here
self.viewBackground.alpha = 0.5;
}
completion:^(BOOL finished){
}];
_isAnimated=NO;
}
else
{
[UIView animateWithDuration:0.3
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
//dismissing background view here
self.viewBackground.alpha = 0.0;
}
completion:^(BOOL finished)
{
}];
_isAnimated=YES;
}
}
For detecting taps outside and dismissing your view:
Create a UITapGestureRecognizer:
#interface ViewController ()
{
UITapGestureRecognizer *tapGesture;
}
Initialize it in ViewDidLoad:
-(void)viewDidLoad
{
//defining your gesture method
tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleSingleTapGesture:)];
//adding gesture recognizer on the background view
[self.backgroundView addGestureRecognizer:tapGestureRecognizer];
}
In your Gesture Recognizer Method:
-(void)handleSingleTapGesture:(UITapGestureRecognizer *)tapGestureRecognizer
{
[UIView animateWithDuration:0.3
animations:^{
//dismissing background view here
self.viewBackground.alpha = 0.0;
}
completion:^(BOOL finished){
}];
}
Hope this helps.
add your code in viewdidload.add QuartzCore framework
#import <QuartzCore/CAAnimation.h>
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
CABasicAnimation *animate = [CABasicAnimation animationWithKeyPath:#"shadowOpacity"];
animate.fromValue = [NSNumber numberWithFloat:1.5];
animate.toValue = [NSNumber numberWithFloat:0.5];
animate.duration = 1.0;
[self.BuyButtonPopUpView.layer addAnimation:animation forKey:#"shadowOpacity"];
self.BuyButtonPopUpView.layer.shadowOpacity = 0.0;
you can add a UIVisualEffectView first then add the popup over this view.
here is a tutorial link https://www.raywenderlich.com/84043/ios-8-visual-effects-tutorial

CALayer Animation Scale with fixed centre on iOS7

I've been banging my head over what I thought would have been an easy thing. A pulsing circle, there are several example on SO, but I'm still unable to fix one last thing.
With the code below, I do have a circle that expand and contracts fine, but during contraction, the scaling is done from the CGRect's layer frame (upper left corner). Expansion works fine, and both expansion/contraction work fine on iOS8. The problem only happens on iOS7.
I tried with setting the anchor point, translating the view while expanding ... nothing seemed to do the trick... I'm doing something wrong, I don't know what.
If anyone want to try with the code below you just need a UIViewController, a label, and a button, wired to the IBOutlets below.
That's my last - known - bug before I can submit my app to the app Store... :\
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (weak, nonatomic) IBOutlet UIButton *button;
#property (nonatomic) BOOL isExpanded;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.isExpanded = YES;
self.label.layer.cornerRadius = self.label.frame.size.width/2;
self.label.layer.masksToBounds = YES;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
- (IBAction)ExpandOrCollapse:(id)sender {
[self.button setEnabled:NO];
CGFloat animationDuration = 0.30f; // Your duration
CGFloat animationDelay = 0.0f; // Your delay (if any)
if (self.isExpanded) {
CGAffineTransform t = CGAffineTransformMakeScale(.17f, .17f);
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:#"transform.scale"];
scaleAnimation.duration = animationDuration;
scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0f];
scaleAnimation.toValue = [NSNumber numberWithFloat:0.17f];
[self.label.layer addAnimation:scaleAnimation forKey:#"scale"];
[UIView animateWithDuration:animationDuration
delay:animationDelay
options: UIViewAnimationOptionCurveEaseIn
animations:^{
}
completion:^(BOOL finished){
self.isExpanded = NO;
[self.label setTransform:t];
[self.button setEnabled:YES];
}];
}else{
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:#"transform.scale"];
scaleAnimation.duration = animationDuration;
scaleAnimation.fromValue = [NSNumber numberWithFloat:0.17f];
scaleAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[self.label.layer addAnimation:scaleAnimation forKey:#"scale"];
[UIView animateWithDuration:0.3
delay:0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
}
completion:^(BOOL finished){
[self.button setEnabled:YES];
[self.label setTransform:CGAffineTransformMakeScale(1.0, 1.0f)];
self.isExpanded = YES;
}];
}
}
#end
EDIT
for eiran, here's the code with his modifications. Problem is still present unfortunately..
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (weak, nonatomic) IBOutlet UIButton *button;
#property (nonatomic) BOOL isExpanded;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.isExpanded = YES;
self.label.layer.cornerRadius = self.label.frame.size.width/2;
self.label.layer.masksToBounds = YES;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
- (IBAction)ExpandOrCollapse:(id)sender {
[self.button setEnabled:NO];
CGFloat animationDuration = 0.30f; // Your duration
CGFloat animationDelay = 0.0f; // Your delay (if any)
if (self.isExpanded) {
[UIView animateWithDuration:animationDuration
delay:animationDelay
options: UIViewAnimationOptionCurveEaseIn
animations:^{
[self.label setTransform:CGAffineTransformScale(CGAffineTransformIdentity, 0.17f, 0.17f)];
}
completion:^(BOOL finished){
self.isExpanded = NO;
[self.button setEnabled:YES];
}];
}else{
[UIView animateWithDuration:0.3
delay:0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
[self.label setTransform:CGAffineTransformScale(CGAffineTransformIdentity, 1.0f, 1.0f)];
}
completion:^(BOOL finished){
[self.button setEnabled:YES];
self.isExpanded = YES;
}];
}
}
This seemed to work for me:
[UIView animateWithDuration:0.75
delay:0
options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction
animations:^{
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.95, 0.95);
}completion:^(BOOL b){
}];
that's a repeating scale animation.
EDIT:
OK then:) apple works in mysterious ways.
i've found a solution for you, but i'm not sure what the problem is.
in ios 7.1, if you work with a storyboard, the scaling does what you say.
BUT, if you add a component dynamically, the animation works fine. add this to your code and check it out:
- (void)viewDidLoad {
[super viewDidLoad];
self.isExpanded = YES;
UIButton* b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setFrame:CGRectMake(100, 400, 120, 40)];
[b addTarget:self action:#selector(expand:) forControlEvents:UIControlEventTouchUpInside];
[b setBackgroundColor:[UIColor blueColor]];
[b setTitle:#"added dynamically" forState:UIControlStateNormal];
[self.view addSubview:b];
}
-(void)expand:(UIButton*)sender{
CGFloat animationDuration = 0.30f; // Your duration
CGFloat animationDelay = 0.0f; // Your delay (if any)
if (self.isExpanded) {
[UIView animateWithDuration:animationDuration
delay:animationDelay
options: UIViewAnimationOptionCurveEaseIn
animations:^{
[sender setTransform:CGAffineTransformScale(CGAffineTransformIdentity, 0.8f, 0.8f)];
}
completion:^(BOOL finished){
self.isExpanded = NO;
}];
}else{
[UIView animateWithDuration:0.3
delay:0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
[sender setTransform:CGAffineTransformScale(CGAffineTransformIdentity, 1.0f, 1.0f)];
}
completion:^(BOOL finished){
self.isExpanded = YES;
}];
}
}
as you said, it was fixed on ios 8.
i thought it might have something to do with constraints, but it looks like it doesn't.
added a project that demonstrates this test here:
the test
hope that helps, eiran.

How to recognize tapping on the whole oscillating SubView to perform interactive animation

In the code below, the subview (cloud) oscillates on the location given.
On tapping that subview while oscillation, it moves out of the bounds to the right side, then moves in from the left.
But the tap gesture is not working on the whole subview, it works only on the right-side end of the oscillating subView.
I want slide out and in of cloud to work, whenever the whole subview of cloud is tapped.
Below is code of .h and .m file respectively.
File : OscillatingCloudsViewController.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface OscillatingCloudsViewController : UIViewController
{
IBOutlet UIView *movingCloud1;
UIImage *cldImg;
}
- (IBAction)animateCloud;
- (IBAction)animateCloudBegin;
#end
File : OscillatingCloudsViewController.m
#import "OscillatingCloudsViewController.h"
#implementation OscillatingCloudsViewController
- (void) viewWillAppear:(BOOL)animated
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureAnimateCloud)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
tapGesture.cancelsTouchesInView = YES;
[movingCloud1 addGestureRecognizer:tapGesture];
movingCloud1.userInteractionEnabled = YES;
[super viewWillAppear:
animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self animateCloud];
}
- (IBAction) animateCloud
{
cldImg = [UIImage imageNamed:#"cloud1.png"];
movingCloud1=[[UIView alloc]initWithFrame:CGRectMake(50, 50, cldImg.size.width, cldImg.size.height)];
[movingCloud1 setBackgroundColor:[UIColor colorWithPatternImage:cldImg]];
[self.view addSubview:movingCloud1];
movingCloud1.userInteractionEnabled = YES;
[self viewWillAppear:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:5];
[UIView setAnimationRepeatCount:HUGE_VALF];
[UIView setAnimationRepeatAutoreverses:YES];
CGPoint pos = movingCloud1.center;
pos.x = 220.0f;
movingCloud1.center = pos;
[UIView commitAnimations];
}
- (void) animateCloudHidden
{
[movingCloud1 setHidden:YES];
}
- (IBAction)animateCloudBegin
{
movingCloud1.frame = CGRectMake(-100, 50, cldImg.size.width, cldImg.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationRepeatAutoreverses:NO];
CGPoint pos = movingCloud1.center;
pos.x = cldImg.size.width;
movingCloud1.center = pos;
[UIView commitAnimations];
}
- (IBAction) tapGestureAnimateCloud
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationRepeatAutoreverses:NO];
movingCloud1.center = CGPointMake(1100.0f, 81.5f);
[UIView commitAnimations];
[self performSelector:#selector(animateCloudBegin) withObject:nil afterDelay:2.0f];
[self performSelector:#selector(animateCloudHidden) withObject:nil afterDelay:3.0f];
[self performSelector:#selector(animateCloud) withObject:nil afterDelay:3.0f];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
I have understood your problem. In viewwillApper you are assing the Gesture and in inside animateCloud method your allocating your movingCloud1 UIView.
For implementing this you need to fallow the bello steps.
In ViewdidLoad you need to allocate and add the movingCloud1 view to you self.view.
After add the Gesture to movingCloud1 view.
Your tapGesture will work now. So inside your tapgesture method just you need to set the animation for your movingCloud1 view

Move UIImageView from A to B

I need to move an UIImageView from x,y to x,y1.
This is what i have. I tried lots of different codes in -(void)animation, most using animateWithDuration but none seems to work. It just doesn't do anything. I must be missing something. Is .h correct? How would you make the image move? I then have to loop that, how can i do that? It's my first time with animate, I can't figure out what to do, it has to be simple. Thanks in Advance.
in .h I connected it this way
#import <UIKit/UIKit.h>
#interface ImageBeaconViewController : UIViewController {
__weak IBOutlet UIImageView *blueView;
}
#end
the in .m I've got
- (void)viewDidLoad
{
[self animation];
[super viewDidLoad];
}
....
- (void)animation{
[UIView animateWithDuration:3 animations:^{
blueView.alpha = 1;
blueView.center = CGPointMake(blueView.center.x , blueView.center.y +??);
}];
}
??= I still have to decide how much I want it to move
This way the app crashes as soon as I get to the screen with the animation
Add Method Like This:
- (void) animateToPoint:(CGPoint)point {
[UIView animateWithDuration:3 animations:^{
blueView.alpha = 1;
blueView.center = point;
}];
}
Call like this:
- (void) viewDidAppear:(BOOL)animated {
int distanceToSlide = 40; // will slide down 40px (use -40 to go up)
CGPoint targetPoint = CGPointMake(blueView.center.x, blueView.center.y + distanceToSlide);
[self animateToPoint:targetPoint];
}
NOTE
Notice that I put your animation call in viewDidAppear if you animate in viewDidLoad you won't see anything because your view isn't displayed yet.
If it's still not moving, make sure "useAutolayout" is not selected check this:
Just because I already built it, try this for tap to animate:
In your viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];
tap.numberOfTapsRequired = 1;
[tap addTarget:self action:#selector(handleTap:)];
[self.view addGestureRecognizer:tap];
}
Then, use these:
- (void) handleTap:(UITapGestureRecognizer *)tap {
int distanceToSlide = 40; // will slide down 40px (use -40 to go up)
CGPoint targetPoint = CGPointMake(blueView.center.x, blueView.center.y + distanceToSlide);
[self animateToPoint:targetPoint];
}
- (void) animateToPoint:(CGPoint)point {
[UIView animateWithDuration:3 animations:^{
blueView.alpha = 1;
blueView.center = point;
}];
}
Here's a way to loop it:
#implementation ImageBeaconViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
pointA = CGPointMake(40, 40);
pointB = CGPointMake(250, 350);
blueView.center = pointA;
}
- (void) viewDidAppear:(BOOL)animated {
[self animate];
}
- (void) animate {
if (CGPointEqualToPoint(_blueView.center, pointA)) {
[self animateToPoint:pointB];
}
else {
[self animateToPoint:pointA];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) animateToPoint:(CGPoint)point {
[UIView animateWithDuration:1.0 animations:^{
_blueView.alpha = 1;
_blueView.center = point;
} completion:^(BOOL finished) {
if (finished) {
[self animate];
}
}];
}
#end
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
blueView.alpha = 1;
blueView.center = CGPointMake(blueView.center.x , blueView.center.y + 100);
} completion:^(BOOL finished) {
//if you need any thing after animation is done
}];
this is them code for animation i have

Display animating view within ViewController

EDITED:
I had created a slide in menu for my view and it works as i wanted, this was the original guide that I used.
http://www.youtube.com/watch?v=79ZQDzzOHLk
My goal was to get this programmed once in a class and get it to work on any view controller that i decided to call it in.
Thanks to #harsh.prasad and some additional research I have managed to get this to work to a point where it works as I want apart apart from linking buttons on to it.
So to update this question in the hope it may help someone else.
This is what I did:
I created a UIView class and called it MenuOne.
MenuOne.h
#import <UIKit/UIKit.h>
#interface TFMenuOne : UIView {
// Pop Up Menu
IBOutlet UIScrollView *scrollView;
IBOutlet UIButton *openMenu;
int draw1;
IBOutlet UIButton *backButton;
}
// Pop Up Menu
- (IBAction)openMenu_clicked:(id)sender;
// Reset draw1 to 0
- (void) resetView: (id) sender;
#property (retain, nonatomic) IBOutlet UIScrollView *scrollView;
#end
MenuOne.m
#import "TFMenuOne.h"
#implementation TFMenuOne
#synthesize scrollView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
draw1 = 0;
scrollView = [[UIScrollView alloc] init];
[scrollView setBackgroundColor:[UIColor whiteColor]];
[scrollView setFrame:CGRectMake(0, 315, 568, 5)];
[scrollView setContentSize:CGSizeMake(568, 5)];
backButton = [[UIButton alloc] init];
[backButton setBackgroundColor:[UIColor greenColor]];
backButton.frame = CGRectMake(224, 350, 120, 30);
openMenu = [[UIButton alloc] init];
[openMenu setBackgroundImage:[UIImage imageNamed:#"menu-button-#2.png"]
forState:UIControlStateNormal];
openMenu.adjustsImageWhenHighlighted = NO;
[openMenu addTarget:self
action:#selector(openMenu_clicked:)
forControlEvents:UIControlEventTouchUpInside];
openMenu.frame = CGRectMake(256, 269, 64, 46);
[self addSubview:scrollView];
[self addSubview:backButton];
[self addSubview:openMenu];
}
return self;
}
// Allow for touch even through transparent View class
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
for (UIView *view in self.subviews) {
if (!view.hidden && view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event])
return YES;
}
return NO;
}
- (void) resetView: (id) sender {
draw1 = 1;
[self openMenu_clicked:sender];
}
- (IBAction)openMenu_clicked:(id)sender {
if (draw1 == 0) {
draw1 = 1;
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
scrollView.frame = CGRectMake(0, 260, 568, 60);
} completion:^(BOOL finished) {
}];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
openMenu.frame = CGRectMake(256, 214, 64, 46);
} completion:^(BOOL finished) {
}];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
backButton.frame = CGRectMake(224, 275, 120, 30);
} completion:^(BOOL finished) {
}];
} else {
draw1 = 0;
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
scrollView.frame = CGRectMake(0, 315, 568, 5);
} completion:^(BOOL finished) {
}];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
openMenu.frame = CGRectMake(256, 269, 64, 46);
} completion:^(BOOL finished) {
}];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
backButton.frame = CGRectMake(224, 350, 120, 30);
} completion:^(BOOL finished) {
}];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
After a lot of trial and error, in order to get this UIView class to appear on multiple ViewControllers I call the view like this in the m file of the view controller. The obstacles I ran in to was that the menu would open but when I left the view controller to go to another view controller the menu would be in the state it was in when i left, it wouldn't reset back to closed. The code below covered that thanks again to #harsh.prasad. I also managed to get the menu to animate in.
#interface TFMapViewController ()
{
TFMenuOne *menuView;
}
#end
#implementation TFMapViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[menuView resetView:nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
menuView = [[TFMenuOne alloc] initWithFrame:CGRectMake(0, 51, 568, 320)];
[self.view addSubview:menuView];
}
- (void) viewDidAppear:(BOOL)animated
{
[UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
menuView.frame = CGRectMake(0, 0, 568, 320);
} completion:^(BOOL finished) {
}];
}
- (void) viewDidDisappear:(BOOL)animated
{
[menuView resetView:nil];
[UIView animateWithDuration:0.0 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
menuView.frame = CGRectMake(0, 51, 568, 320);
} completion:^(BOOL finished) {
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// Allows for Exit button to work
- (IBAction)returned:(UIStoryboardSegue *)segue {
}
#end
I you want you can use tabsController if that kind of feature you want, otherwise create a class of your slide in menu view and then you can use it in all of your views as you like. No same piece of code is to be rewritten.
EDIT:
Suppose you have created a class CustomMenuView which basically creates your menu view. So now you can use this in every view controller you want to use it in in the following way:
CustomMenuView *menuView = [CustomMenuView alloc] initWithFrame:CGRectMake(0, 200, 320, 100);
menuView.<properties you want to pass> = <set properties here>;
[self.view addSubView:menuView];
This will set the view with the custom menu and then you can handle the actions in this menu.
1)Here is an old tutorial for creating menu - http://www.raywenderlich.com/32054/how-to-create-a-slide-out-navigation-like-facebook-and-path/projectlayout
2)A better way is to create Drawer menu With container views. You can read more about container views from WWDC videos.
3)Or if you are lazy to d it yourself, try this library http://cocoacontrols.com/platforms/ios/controls/jtrevealsidebar
P.S. No , you do not havev to repeat the code.
You can try this,
First make a view on the origin point x = 0 y = 480 (for iphone4) and then run this code.
CGRect theFrame = self.viewMenuShare.frame;
theFrame.origin = CGPointMake(0, 480);
self.viewMenuShare.frame = theFrame;
theFrame.origin = CGPointMake(0,480 - 187);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
self.viewMenuShare.frame = theFrame;
[UIView commitAnimations];

Resources