Animation on UIButton Click in iPhone - iphone

Everybody..
How to set animation on button click ?
I have two buttons, both have different frame..
First CGRectMake (10 , 10 , 100 , 80 ); It has Black Image,
Second CGRectMake (200, 10 , 210 , 80 ); It has White Image,
When I click on First button of those, It should be animate to move from one frame to another frame..
Only seen like first's button Black Image move to second button location.. Like Image changes of Buttons..
After clicked button frame does not change... Should be remain as First and Second..
I have tried but not got the exact
buttonFirst.frame = buttonSecond.frame;
buttonFirst.center = buttonSecond.center;
buttonFirst.transform = CGAffineTransformMakeTranslation( buttonSecond.frame.origin.x - buttonFirst.frame.origin.x , buttonSecond.frame.origin.y - buttonFirst.frame.origin.y);
How to animate this ?
Editting :
I need to move one checkers from one CGRect to Another CGRect.. So, It want to display like moving checkers, But i dont want to change CGRect, Only button images looks like that...
Please tell me..
Thanks...

very simple logic
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.20f];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationBeginsFromCurrentState:YES];
[firstButton setFrame:CGRectMake(5, 50, 30, 30)];//moves to second button place
[secondButton setFrame:CGRectMake(5, 5, 30, 30)];//moves to first button place
[UIView commitAnimations];
use the logic accordingly to your use..

well..
you can try this link
[UIView beginAnimation/endAnimation] bock

CGRect oldRectFirst = buttonFirst.frame;
CGRect oldRectSecond = buttonSecond.frame;
[UIView animateWithDuration:0.2f animations:^{
buttonFirst.frame = oldRectSecond;
buttonSecond.frame = oldRectFirst;
}];

Here is example from working program:
[self.buttonFirst setUserInteractionEnabled:NO];
[UIView transitionWithView:self.buttonFirst
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft | UIViewAnimationOptionCurveLinear
animations:^{
buttonFirst.frame = buttonSecond.frame;
buttonFirst.center = buttonSecond.center;
}
completion:^(BOOL finished){
[self.buttonFirst setUserInteractionEnabled:YES];
}];

Finally i got the answer...
I have set two button in two separate view. after set one timer, and move first button image to another button's position..
Here is a sample code for that..
Step 1 : Set view with Timer
UIView *oldView = [[UIView alloc] init];
oldView = [viewArray objectAtIndex:selectedIndex];
UIView *newView = [[UIView alloc] init];
newView = [viewArray objectAtIndex:[sender tag]];
oldRect = [oldView frame];
newRect = [newView frame];
movingTimer = [[NSTimer alloc] init];
movingTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(movingCheckers:) userInfo:nil repeats:YES];
imageViewMove = [[UIImageView alloc] init];
[imageViewMove setFrame:oldRect];
[imageViewMove setImage:[UIImage imageNamed:blackManName]];
[self.view bringSubviewToFront:imageViewMove];
[self.view addSubview:imageViewMove];
Step 2 : Moving Image to second position
CGPoint aPoint = CGPointMake(newRect.origin.x - oldRect.origin.x, oldRect.origin.y - newRect.origin.y);
imageViewMove.frame = CGRectMake(imageViewMove.frame.origin.x + (aPoint.x / 6) , imageViewMove.frame.origin.y - (aPoint.y / 6), imageViewMove.frame.size.width, imageViewMove.frame.size.height);
if (imageViewMove.frame.origin.x >= newRect.origin.x)
{
[selectButton setBackgroundImage:nil forState:UIControlStateNormal];
[moveButton setBackgroundImage:imageViewMove.image forState:UIControlStateNormal];
[imageViewMove removeFromSuperview];
[movingTimer invalidate];
}
Now its work.....

Related

Flip SubView on UIScrollView not working

I'm coding an iPhone TabBar Project with UiNavigation, the first view contain UIScrollView and the scrollview contain views every subview contain imageview,anUiImage and a UiButton follows
is the function that build the first screen
//*******************************************************
//* Build the main screen *
//*******************************************************
-(void) buildScreen{
sing.viewsArray = [[NSMutableArray alloc]init];
int Vve = 10;
int Vho = 10;
int wi = 95;
int hi = 95;
int ArrayIndex = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
staticView = [[UIView alloc] initWithFrame:CGRectMake(Vve, Vho, 100, 100)];
[staticView setTag:ArrayIndex];
staticImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[sing.stdPicArray objectAtIndex:ArrayIndex]]];
[staticImage setFrame:CGRectMake(0, 0, wi, hi)];
[staticView addSubview:staticImage];
viewButton = [UIButton buttonWithType:UIButtonTypeCustom];
viewButton.frame = CGRectMake(0, 0, wi, hi);
[viewButton addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[viewButton setTag:ArrayIndex];
[sing.buttonsArray addObject:viewButton];
[staticView addSubview:viewButton];
[sing.viewsArray addObject:staticView];
[MainScroll addSubview:[sing.viewsArray objectAtIndex:ArrayIndex]];
Vve += 100;
ArrayIndex++;
}
Vve = 10;
Vho += 100;
}
}
When I click on the UiButton I want to add another UIView to the UIScrollView and make a "UIViewAnimationTransitionFlipFromLeft" when the view is added to the scroll view
tried the following code
-(void)animateFlip{
[UIView transitionWithView:flipViewBack
duration:1.0
options:UIViewAnimationTransitionFlipFromLeft
animations:^{ [self.view addSubview:flipViewBack]; }
completion:^(BOOL f){ }];
}
didn't work just added the flipViewBack
-(void)animateFlip{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.flipViewFront
cache:YES];
[MainScroll addSubview:flipViewFront];
[UIView setAnimationDidStopSelector:#selector(flipAnimationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
didn't work just like the other one, the next code did flip the main view with the view I wanted, but it didn't flip only the the view I want
-(void)animateFlip{
[UIView beginAnimations:nil
context:nil];
[UIView setAnimationDuration:1.0];
[UIView transitionFromView:self.view
toView:flipViewBack
duration:2
options:UIViewAnimationOptionTransitionFlipFromBottom
completion:NULL];
[UIView setAnimationDidStopSelector:#selector(flipAnimationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
//set transformation
[UIView commitAnimations];
}
Does any body know how to do it' I looked everywhere and all the code I found didn't work for me
Thanks
I tried another option suggested in this link
iPhone subview flip between 2 views and it didn't work for me
this is the function that I use on click in the last 3 lines im adding to the flipView (container) the flipViewFront view then I add the flipView to the mainScroll view and then I go to execute the animation in the animateFlip that I copied from your sample
//*******************************************************
//* Click on the picture as button *
//*******************************************************
-(void) buttonClick:(id) sender{
UIButton *button = (UIButton *)sender;
int row = [button superview].tag;
NSLog(#"Button pressed tag: %i",row);
self.flipView = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipView.backgroundColor = [UIColor clearColor];
self.flipViewBack = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipViewFront = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipViewFront.backgroundColor = [UIColor whiteColor];
self.flipViewImage = [UIImage imageNamed:[sing.stdPicArray objectAtIndex:row]];
self.filpImage = [[UIImageView alloc]initWithImage:self.flipViewImage];
[self.flipViewBack addSubview:self.filpImage];
self.flipCloseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.flipCloseButton setFrame:CGRectMake(0, 0, 24, 24)];
[self.flipCloseButton setImage:[UIImage imageNamed:#"close_button.png"] forState:UIControlStateNormal];
[self.flipCloseButton addTarget:self action:#selector(closwFlip:) forControlEvents:UIControlEventTouchUpInside];
[self.flipViewBack addSubview:flipCloseButton];
self.flipOkButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 0, 190, 190)];
[self.flipCloseButton addTarget:self action:#selector(continueTo:) forControlEvents:UIControlEventTouchUpInside];
[self.flipViewBack addSubview:flipOkButton];
[flipView addSubview:flipViewFront];
[MainScroll addSubview:flipView];
[self animateFlip];
}
found the problem, but not the answer, if it works directly from the ViewDidLoad it and the first view is on screen all is well and the flip works' if I change it so that the want to add a view and flip it in one event it shows me the second(flipped) view without the flip
Your problems seems to be that you are trying to animate the flip on the view before it is added to the view hierarchy.
Instead you should add the back view to a common container view (that later will contain the front view) and then flip the container view while adding the front and removing the back, like this:
// Container view is a view that is already added to your view hierarchy.
// It contains the back view. The front view will be added to it.
[UIView transitionWithView:containerView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[backView removeFromSuperview]; // This is already added to the container
[containerView addSubview:frontView]; // Not yet added ...
}
completion:NULL];
Update
This is all the code I wrote when answering this question. It works for me. If it still doesn't animate for you then you probably haven\t set up your view hierarchy the same way.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
containerView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
[self.view addSubview:containerView];
frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[frontView setBackgroundColor:[UIColor orangeColor]];
backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[backView setBackgroundColor:[UIColor redColor]];
[containerView addSubview:backView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(flip)];
[self.view addGestureRecognizer:tap];
}
- (void)flip {
[UIView transitionWithView:containerView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[backView removeFromSuperview];
[containerView addSubview:frontView];
}
completion:NULL];
}

How to do the move the images like marquee in html in iphone

i am developing one application.In that i have 10 images .i want to move that images from right to left like as a marquee effect in HTML.That images are moving infinite number of times.So please tell me how to move that images from left to right.
I'd try something like this:
- (void)startAnimatingImages
{
for (UIImage* aImage in yourImageArray)
{
[self animateLabelToTheRight:aImage];
}
}
- (void)animateLabelToTheRight:(UIView *)yourView
{
[UIView animateWithDuration:0.3
animations:^{ view.frame = CGRectMake(view.frame.origin.x + 35, view.frame.origin.y, image.frame.size.width, image.frame.size.height); }
completion:^{ [self animateLabelToTheLeft:yourView] } ];
}
- (void)animateLabelToTheLeft:(UIView *)yourView
{
[UIView animateWithDuration:0.3
animations:^{ yourView.frame = CGRectMake(yourView.frame.origin.x - 35, yourView.frame.origin.y, yourView.frame.size.width, yourView.frame.size.height); }
completion:^{ [self animateLabelToTheRight:yourView] } ];
}
As stated in #Dev's answer this can be done through animation, however if you're going to animate it theres no need to change the image if you only want to move it.
This code for example takes a photo of your choosing and moves it from the top left of the screen to the bottom left and can be modified to move right to left, and is already set up to repeat infinitely with [UIView setAnimationRepeatCount:-1];.
In achieving a marquee effect, the best way I can think of to handle that would be to make the images fully animate off screen before the animation ends and resets, creating the illusion that the image went off one side of the screen and came in the other.
- (void)animateImage
{
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.png"]];
[self.view addSubview:myImageView];
[myImageView setFrame:CGRectMake(0, 0, 50, 50)];
[UIView beginAnimations:#"myAnimation" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationRepeatCount:-1];
// [UIView setAnimationDidStopSelector:#selector(callSomeThingElse)];
[myImageView setFrame:CGRectMake(0, 430, 50, 50)];
[UIView commitAnimations];
}
Animation is required for this type of moving image. if you try this code ,your problen is solve:
UIImageView *animationimage;
animationimage.animationImages = [NSMutableArray arrayWithObjects:
[UIImage imageNamed:#"Splash1.png"],
[UIImage imageNamed:#"Splash2.png"],
[UIImage imageNamed:#"Splash3.png"],
[UIImage imageNamed:#"Splash4.png"],
[UIImage imageNamed:#"Splash5.png"],nil];
// all frames will execute in 1.0 seconds
animationimage.animationDuration = 1.0;
// repeat the annimation forever
animationimage.animationRepeatCount = 0;
remember one thing you have different sequential image is require link in my code splash%i.
i hope ,helpful for you.

Animated moving UIButton not responding to touches/taps while moving

I'm trying to animate a UIButton to move up the screen. At any point the user can touch it. However, it doesn't seem to respond to touches while moving, only at the start and end of its animation. I guess this is because the button itself isn't moving, just the image of it. Any ideas how I can solve this? Here's my code so far. Thanks!
- (void)viewDidLoad {
[self newBubble];
[super viewDidLoad];
}
- (void)newBubble {
UIButton *bubble = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
bubble.frame = CGRectMake(10, 380, 50, 50);
UIImage *bubbleImage = [UIImage imageNamed:#"bubble.png"];
[bubble setBackgroundImage:bubbleImage forState:UIControlStateNormal];
bubble.imageView.contentMode = UIViewContentModeScaleAspectFit;
[bubble addTarget:self action:#selector(bubbleBurst:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:bubble];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.0];
CGAffineTransform newTransform = CGAffineTransformMakeScale(1.5, 1.5);
bubble.transform = CGAffineTransformTranslate(newTransform, 10, -200);
[UIView commitAnimations];
}
- (IBAction)bubbleBurst:(id)sender {
NSLog(#"Bubble burst");
UIButton *bubbleBurst = sender;
[bubbleBurst removeFromSuperview];
}
The +[UIView animateWithDuration:delay:options:animations:completion:] method takes a UIViewAnimationOptions argument. If you include UIViewAnimationOptionAllowUserInteraction, you should be able to press the button while it animates:
[UIView animateWithDuration:5.0
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
CGAffineTransform newTransform = CGAffineTransformMakeScale(1.5, 1.5);
bubble.transform = CGAffineTransformTranslate(newTransform, 10, -200);
} completion:nil];
Well after posting a similar question, I found the answer here
Basically, you have to move the button frame by frame using an NSTimer. In my case I was worried there would be too many NSTimers involved moving each button, but in the end I used a single timer which loops through an array of buttons and moves them all one by one.
I'm pretty sure you would have to use Core Animation if you want the button to respond to touches while it is moving, but I have never done this so this is really more a tip then an answer!
I agree. I think the problem is that you're not using Core Animation. I can't believe you couldn't figure that out for yourself.

moving UILabel upside with NSTimer

HI all,
i want to move a UILabel up for 5 sec and gets hidden.
Something we see in games, when we get bouus or something,
a text comes up ,moves up saying something "+300" etc.
How to do move UIlabel?
regards
Have a look at the UIView animation methods
i.e.
- (void)showScoreLabelFor:(int)score {
// Make a label
UILabel *scoreLabel = [UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)] autorelease];
// Set it's text to the score we want to show
[scoreLabel setText:[NSString stringWithFormat:#"%+i", score]];
// Center it in the view
[scoreLabel sizeToFit];
[scoreLabel setCenter:CGPointMake([[self view] center].x, 200)];
// Add it to the view
[[self view] addSubview:scoreLabel];
// Animate it up 100 pixels ver 2 seconds
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
CGRect frame = [scoreLabel frame];
frame.origin.y -= 100;
[scoreLabel setFrame:frame];
[UIView commitAnimations];
}

How to add a moving text in iPhone

I want to add some text which is a long text lets say a story and I want it to be presented in a moving up fashion. That is it will be displayed in a box and the text will be moved line by line to show the whole story. So one line gets out of the box from top and other comes from the bottom. Like a HTML marquee behavior.
You should look into whether or not UIScrollView's contentOffset property is animatable using UIView animation blocks.
If it's not, then you could try using an NSTimer and in its function, either set the contentOffset or call [scrollView scrollRectToVisible:desiredContentFrame animated:YES]; where desiredContentFrame.origin.y is just a little bit lower than what you previously had it at (depending on the scroll speed you want). Then play around with the firing frequency of your timer and the desiredContentFrame.origin.y's delta to see what looks best and has the best performance.
Here's a quick hack showing both ways:
- (void)startAnimTimer
{
currentRect = CGRectMake(0, 0, 1, 1);
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(doScroll) userInfo:nil repeats:YES];
}
- (void)startAnimUIView
{
[UIView beginAnimations:#"scrollUIViewAnim" context:nil];
[UIView setAnimationDuration:20.0];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
CGPoint newContentOffset = CGPointMake(0, scrollView.contentSize.height);
scrollView.contentOffset = newContentOffset;
[UIView commitAnimations];
}
- (void)loadView {
[super loadView];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(5, 5, 200, 200)];
scrollView.contentSize = CGSizeMake(200, 1000);
scrollView.backgroundColor = [UIColor magentaColor];
CGFloat y = 10;
CGFloat yDelta = 21;
for(int i = 0; i < 47; i++)
{
// Add some UILabels to the scrollview just so we can see it animating
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, y, 180, 20)];
label.text = [NSString stringWithFormat:#"Cheezburger %d", i];
[scrollView addSubview:label];
[label release];
y += yDelta;
}
[self.view addSubview:scrollView];
// Comment out one of these
//[self startAnimTimer];
[self startAnimUIView];
}
- (void)doScroll
{
CGFloat scrollDelta = 1.0f;
CGRect newRect = CGRectMake(0, currentRect.origin.y + scrollDelta, currentRect.size.width, currentRect.size.height);
// Scroll the scrollview to the next position
[scrollView scrollRectToVisible:newRect animated:YES];
currentRect = newRect;
}
This method may help (or not):
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
If not, just make a big UILabel and animate its frame using a CABasicAnimation.
I think you want to look at the Scroll View (UIScrollView) in the iPhone SDK.
http://developer.apple.com/IPhone/library/documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html
Depending on your implementation, you may not be able to use it "out of the box" but it should provide the functionality you seem to be describing. I'm not sure it will do the auto-scroll thing but that should be pretty easy to implement... maybe.... But, I'm still confident it could be done.