How to flip an imageview in the UIAlertView in objective c - iphone

I have an AlertView with an imageView and I have to show the image in flip animation (UIViewAnimationTransitionFlipFromLeft/Right) as a subview of alert view
I am using the following method
- (void) setAnimationFlip:(UIAlertView*)alertView {
UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(20, 40, 150, 150)];
UIImageView *mainView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"scene1.jpg"]];
UIImageView *flipToView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"scene2.jpg"]];
mainView.frame = CGRectMake(20, 40, 200, 210);
flipToView.frame = CGRectMake(20, 40, 200, 210);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
[UIView setAnimationTransition:([mainView superview] ?
UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
forView:containerView cache:YES];
if ([flipToView superview])
{
[flipToView removeFromSuperview];
[containerView addSubview:mainView];
}
else
{
[mainView removeFromSuperview];
[containerView addSubview:flipToView];
}
[self.alrtViewPreview addSubview:containerView];
[self.alrtViewPreview show];
[UIView commitAnimations];
//[containerView release];
//[mainView release];
//[flipToView release];
}
and calling like...
switch (iEffect) {
case 1:
[self setAnimationFlip:alrtViewPreview];
break;
}
but it is not working for me, can any one help me?

-(IBAction)btnClick:(UIButton*)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:sender cache:YES];
[UIView setAnimationDelegate:self];
[sender setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",self.tileImage]] forState:UIControlStateNormal];
[sender setTitle:self.tileText forState:UIControlStateNormal];
[UIView commitAnimations];
}

Related

UILabels Marquee + Overlapping while animating labels

I wrote following code for adding marquee effect on labels being shown one bye one on a view.
- (void)marqueeMessage:(NSString *)messageString
{
UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(520, 0, 480, 21))];
label.text = messageString;
[self.view addSubview:label];
[UIView animateWithDuration:0.2
animations:^ {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:20];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
label.frame = CGRectMake(-480, 0, 480, 21);
[UIView commitAnimations];
}
completion:^(BOOL finished) {
NSLog(#"Animation Done!");
if (array.count > 0)
{
nextIndex++;
NSString *strMessage = [array objectAtIndex:nextIndex];
[self marqueeMessage:strMessage];
}
}];
}
Some how, strings in array are being displayed in such a manner, that they are overlapping while performing animation.
Any idea, anyone???
Let me know, in case need more information.
-(void)viewDidLoad {
[super viewDidLoad];
array=[[NSArray alloc]initWithObjects:#"Scroll test",#"Scroll test1",#"Scroll test2",#"Scroll test3",#"Scroll test4",nil];
[self marqueeMessage:[array objectAtIndex:0]];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)marqueeMessage:(NSString *)messageString {
label = [[UILabel alloc] initWithFrame:(CGRectMake(0, 50, 90, 21))];
//label.tag=nextIndex;
label.text = messageString;
[self.view addSubview:label];
[UIView beginAnimations:#"LBL" context:nil];
[UIView setAnimationDuration:3];
[UIView setAnimationDidStopSelector:#selector(performThis:)];
[UIView setAnimationDelegate:self];
label.frame = CGRectMake(360,50,90,21);
[UIView commitAnimations];
}
- (void) performThis:(id) sender {
if (i<[array count]) {
label.text = [array objectAtIndex:i];
i++;
}
else
{
i=0;
label.text = [array objectAtIndex:i];
}
label.frame = CGRectMake(-90,50,90,21);
[UIView beginAnimations:#"LBL" context:nil];
[UIView setAnimationDuration:3];
label.frame = CGRectMake(360,50,90,21);
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(performThis:)];
[UIView commitAnimations];
}
may help you.
I think you're never removing labels from superview... you need to keep reference to your label and do so :
[old_label removeFromSuperview];
It looks to me that your first animation (which lasts 0.2 seconds) finishes and the completion is called, which adds another label to your view, on top of the nested animation which lasts for 20 seconds and is therefore still on the screen, etc, etc. You'll end up with a pile of overlapping labels.

Flip animation not working in GMGridview

I am working in an app in which i have used the GMGridview. I have added the buttons in cellforItems method. -
(GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
NSLog(#"Creating view indx %d", index);
CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
GMGridViewCell *cell = [gridView dequeueReusableCell];
if (!cell)
{
cell = [[GMGridViewCell alloc] init];
cell.deleteButtonIcon = [UIImage imageNamed:#"close_x.png"];
cell.deleteButtonOffset = CGPointMake(-15, -15);
}
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:index];
[btn setFrame:CGRectMake(0, 0,size.width, size.height)];
NSLog(#"button frame is %#",btn);
[[btn layer] setBorderColor:[UIColor redColor].CGColor];
[[btn layer]setBorderWidth:2.0f];
[btn addTarget:self action:#selector(SegmentChange:) forControlEvents:UIControlEventTouchUpInside];
cell.contentView = btn;
//int btntag=btn.tag;
return cell;
then I have used the method to Flip all the buttons,but its not working , What i want is when i click on flip button all the buttons in the gridview should flip.how can i do this ,the code for flip i use is here,
for(ll=0;ll<[Image_array count];ll++)
{
// [UIView setAnimationDelay:2.0];
[UIView beginAnimations:#"BackButton" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:12.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:btn cache:YES];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
[btn setImage:[UIImage imageNamed:nil] forState:UIControlStateNormal];
[arr_check replaceObjectAtIndex:ll withObject:#"uncheck"];
front = NO;
}
[_gmGridView reloadData];
As the FOR loop is very much faster comparing to UIViewAnimation. So even if it is animating your view, you will not able to see the animation effect.
You can use NSTimer Or UIViewAnimation delegate method to determine if the one view flip event is completed or not. Then from the delegate method(of UIViewAnimation) you can start another views animation.
i have removed the code from for loop and used the same code in the cellForItemAtIndexpath and use the cell to flip than the button
here is the code :
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:cell cache:YES];
[UIView setAnimationDuration:0.7];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];

Move UIButton 10px down and open new ViewController

I'm newbie at iOS development and this is my first question on SO.
I want to create "animation".
When I tap UIButton I want to slowely (about 0.5 seconds) move it down for 10px and when I raise my finger I want to open new viewController.
I made something but I don't know how to make "animation".
UIImage *normalImage = [UIImage imageNamed:#"FirstImage"];
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
firstButton.frame = CGRectMake(31, 194, normalImage.size.width, normalImage.size.height);
[firstButton setImage:normalImage forState:UIControlStateNormal];
[firstButton addTarget:self action:#selector(showSecondViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:firstButton];
- (void)showSecondViewController; {
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:secondViewController animated:YES];
[progressViewController release];
}
[UIView animateWithDuration:(NSTimeInterval)duration animations:^(void) {
//put your animation result here
//then it will do animation for you
} completion:^(BOOL finished){
//do what you need to do after animation complete
}];
for example:
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//add firstButton to some view~
//....
firstButton.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:0.5 animations:^(void) {
firstButton.transform = CGAffineTransformMakeTranslation(0,10);
} completion:^(BOOL finished){
show your view controller~
}];
[UIView animateWithDuration:0.5 animations: ^ {
button.frame = CGRectMake:(button.frame.origin.x, button.frame.origin.y + 10.0f, button.frame.size.width, button.frame.size.height);
}];
You can put transformations(repositioning), scaling and rotatations between beginAnimations and commitAnimations blocks where you can specify the AnimationRepeatCount,AnimationCurve etc.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:0.5];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:2];
..........................//etc.
firstButton.frame = CGRectMake(31, 194, normalImage.size.width, normalImage.size.height);
[UIView commitAnimations];
But in iOS4 and later animations are encouraged to be performed by
animateWithDuration:delay:options:animations:compeletion and similar methods that invlove blocks which are very powerful. For more information on block animations please refer to apple documentation

Implement animation in UIButton

I'm new to cocoa and iphone programming and I want to implement an animation in UIButton. For example, I create a custom UIButton with a square image. Then when I press that UIButton, the square image will flip. Note that the square image is the image of the UIButton.
[UIButton setImage:[UIImage imageNamed:#"square.png"]];
I had the same problem and I solved it in a very similar way. I just subclassed the UIButton and implemented a method like that:
- (void) flipBackgroundImage:(UIImage*) image
{
[UIView beginAnimations:#"flipbutton" context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];
[self setBackgroundImage:image forState:UIControlStateNormal];
[UIView commitAnimations];
}
Works like a charm.
Cheers,
anka
Below is the complete code for flipping a button (with background image) when it will pressed.
Basically you need two button and a container view.
///// .H file code....
//Container views used for flipping the bars to show whose turn it is
UIButton* btn1;
UIButton* btn2;
UIView *BarContainerView;
///// .M file code....
- (void)viewWillAppear:(BOOL)animated
{
BarContainerView = [[UIView alloc] initWithFrame:CGRectMake(20, 30, 103, 150)];
btn1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain];
[btn1 addTarget:self action:#selector(btn1_click) forControlEvents:UIControlEventTouchUpInside];
[btn1 setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
btn1.frame = CGRectMake(0, 0, 103, 150);
btn2 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain];
[btn2 addTarget:self action:#selector(btn2_click) forControlEvents:UIControlEventTouchUpInside];
[btn2 setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateNormal];
btn2.frame = CGRectMake(0, 0, 103, 150);
[BarContainerView addSubview:btn1];
[self.view addSubview:BarContainerView];
[self.view bringSubviewToFront:BarContainerView];
}
- (void) btn1_click
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:BarContainerView cache:YES];
[btn1 removeFromSuperview];
[BarContainerView addSubview:btn2];
[UIView commitAnimations];
}
- (void) btn2_click
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:BarContainerView cache:YES];
[btn2 removeFromSuperview];
[BarContainerView addSubview:btn1];
[UIView commitAnimations];
}

For iphone development, how to flip a UIImageView?

I want to flip an imageView (left/right), but I can not find a UIView (or UIImageView) method to do that? any idea?
thanks!
I think the only thing you're looking for is:
UIView's
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;
and
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
These animation transitions can only be used within an animation block. The transition is set on the container view and then the old view is swapped out for the new view, then the animation is committed.
Like:
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:yourContainerView cache:YES];
[yourContainerView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView commitAnimations];
Put your UIIMageView into a UIView and use this code (from the utility app project sample)
- (void)loadFlipsideViewController {
FlipsideViewController *viewController = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
self.flipsideViewController = viewController;
[viewController release];
// Set up the navigation bar
UINavigationBar *aNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
aNavigationBar.barStyle = UIBarStyleBlackOpaque;
self.flipsideNavigationBar = aNavigationBar;
[aNavigationBar release];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(toggleView)];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:#"Test123"];
navigationItem.rightBarButtonItem = buttonItem;
[flipsideNavigationBar pushNavigationItem:navigationItem animated:NO];
[navigationItem release];
[buttonItem release];
}
- (IBAction)toggleView {
/*
This method is called when the info or Done button is pressed.
It flips the displayed view from the main view to the flipside view and vice-versa.
*/
if (flipsideViewController == nil) {
[self loadFlipsideViewController];
}
UIView *mainView = mainViewController.view;
UIView *flipsideView = flipsideViewController.view;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([mainView superview] ? UIViewAnimationTransitionFlipFromRight : UIViewAnimationTransitionFlipFromLeft) forView:self.view cache:YES];
if ([mainView superview] != nil) {
[flipsideViewController viewWillAppear:YES];
[mainViewController viewWillDisappear:YES];
[mainView removeFromSuperview];
[infoButton removeFromSuperview];
[self.view addSubview:flipsideView];
[self.view insertSubview:flipsideNavigationBar aboveSubview:flipsideView];
[mainViewController viewDidDisappear:YES];
[flipsideViewController viewDidAppear:YES];
} else {
[mainViewController viewWillAppear:YES];
[flipsideViewController viewWillDisappear:YES];
[flipsideView removeFromSuperview];
[flipsideNavigationBar removeFromSuperview];
[self.view addSubview:mainView];
[self.view insertSubview:infoButton aboveSubview:mainViewController.view];
[flipsideViewController viewDidDisappear:YES];
[mainViewController viewDidAppear:YES];
}
[UIView commitAnimations];
}
mainView and flipToView are objects of imageViews and containerView is an object of UIView.
Given below are two sample code to curl the imageView and to flip the ImageView from left to Right
- (void)curlAction:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:([mainView superview] ? UIViewAnimationTransitionCurlUp : UIViewAnimationTransitionCurlDown)forView:containerView cache:YES];
if ([flipToView superview])
{
[flipToView removeFromSuperview];
[containerView addSubview:mainView];
}
else
{
[mainView removeFromSuperview];
[containerView addSubview:flipToView];
}
[UIView commitAnimations];
}
and to bring the image from left to right here's the code
- (void)flipAction:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:([mainView superview] ?
UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
forView:containerView cache:YES];
if ([flipToView superview])
{
[flipToView removeFromSuperview];
[containerView addSubview:mainView];
}
else
{
[mainView removeFromSuperview];
[containerView addSubview:flipToView];
}
[UIView commitAnimations];
}
Thank &Regards
Another solution is available at my repo but it does'nt involve animation yet! It only manipulates image orientations to provide for some image effects namely flip vertically/horizontally and rotate 90 degrees left/right.