How to set animation to UIImageView? - iphone

How to give animation to UIImageView.
I have a following code for animation .but not working for imageview
MyImage=[[UIImageView alloc] initWithFrame:CGRectMake(10, 57, 220,140)];
MyImage.image=[UIImage imageNamed:#"image.png"];
MyImage.userInteractionEnabled = YES;
[self.view addSubview:MyImage];
[UIView beginAnimations : nil context : nil];
[UIView setAnimationCurve : UIViewAnimationCurveLinear];
[UIView setAnimationDuration : 1.00];
UIView setAnimationTransition : UIViewAnimationTransitionFlipFromLeft forView : MyImage cache : YES];
[UIView commitAnimations];
Anybody have idea let me know ..Thanks

UIView setAnimationTransition : UIViewAnimationTransitionFlipFromLeft forView : MyImage cache : YES];
Loose [ before the code? Maybe it's the type mistake.
And I think you may like this one, block-based animation is better ;) :
[UIView transitionFromView:self.view
toView:MyImage
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
And here's code snippet that works well:
MyImage=[[UIImageView alloc] initWithFrame:CGRectMake(10, 57, 220,140)];
MyImage.image=[UIImage imageNamed:#"NTCHomeMainPic.png"];
MyImage.userInteractionEnabled = YES;
// [self.view addSubview:MyImage]; // You don't need to add it as subview, but never mind
[UIView transitionFromView:self.view
toView:MyImage
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
Edit:
You need to create a new view and add the MyImage to it. New version below:
UIView * newView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
[newView setBackgroundColor:[UIColor whiteColor]];
UIImageView * MyImage=[[UIImageView alloc] initWithFrame:CGRectMake(10, 57, 220,140)];
MyImage.image=[UIImage imageNamed:#"NTCHomeMainPic.png"];
MyImage.userInteractionEnabled = YES;
[newView addSubview:MyImage];
[UIView transitionFromView:self.view
toView:newView
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];

Related

Duplicating Twitterrific Style Alert

I am trying to replicate the screen in the middle, namely the alert that is displayed to compose a new tweet. I have tried to duplicate it with a custom UIAlertView but that requires a lot of subclassing and what not, then I suspected it might just be a simple UIViewController that is displayed over the main view... professional thoughts from fellow iOS developers is needed.
Thank you.
In iPad:
You can do it with a UIViewController.
You need to design UI according to the above image.
Then you need to set the Modal Presentation Style to UIModalPresentationFormSheet
And present it using - (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))
Like:
UIViewController *viewC = [[UIViewController alloc] init];
viewC.view.frame = //set the frame;
viewC.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:viewC animated:YES completion:nil];
In iPhone:
You can do it with using a UIView.
You can use the following code for doing this:
Declare a property in your interface
#property (nonatomic, strong) UIView *views;
//Add the view
- (void) showView
{
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 35)];
UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(remove)];
toolbar.items = [NSArray arrayWithObject:bar];
_views = [[UIView alloc] initWithFrame:CGRectMake(0, -300, self.view.bounds.size.width, self.view.bounds.size.height/3)];
[_views addSubview:toolbar];
[_views setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:_views];
[UIView animateWithDuration:0.7
delay:0.0
options:UIViewAnimationCurveLinear
animations:^{
_views.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height/3);
}
completion:^(BOOL yes){
NSLog(#"YO");
}];
[UIView commitAnimations];
}
//Remove the view
- (void)remove
{
[UIView animateWithDuration:0.7
delay:0.0
options:UIViewAnimationCurveLinear
animations:^{
_views.frame = CGRectMake(0, -500, self.view.bounds.size.width, self.view.bounds.size.height/3);
}
completion:^(BOOL yes){
NSLog(#"YA");
}];
[UIView commitAnimations];
}

how to remove a view?

I have an iphone application in which i am adding a view controllers view with an animation like flipfrom left.I was doing it with navigationbarbutton item.when clicking on it i was adding a view controllers view to my rootview.and changing the button amd in that action i am removing that view and adding the previous button like that.but the pbm i need to remove that info view with in a button action inside that view.here is my code for the flipanimation.`
- (void)backPressed1
{
InfoViewController *infoviewcontroller = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.95];
//[UIView beginAnimations:nil context:NULL];
//[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
//CGRect frame=CGRectMake(0,0,320,480);
[rootview removeFromSuperview];
//[infoviewcontroller.view setFrame:frame];
//selfhelpscoresAppDelegate *appdelegate=[[UIApplication sharedApplication] delegate];
[infoviewcontroller viewWillAppear:YES];
[self.view addSubview:infoviewcontroller.view];
[UIView commitAnimations];
UIImage *buttonImage = [UIImage imageNamed:#"information_btn.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 42, 32);
[button addTarget:self action:#selector(backpressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = customBarItem;
//UILabel *label =[[[UILabel alloc]init]autorelease];
self.navigationItem.title=#"Tutorial";
//label.text = #"";
//self.navigationController.title = #"";
[customBarItem release];
}
-(IBAction)backpressed
{
InfoViewController *infoviewcontroller = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.95];
//[UIView beginAnimations:nil context:NULL];
//DailypercentageViewController *pieChart = [[DailypercentageViewController alloc] initWithNibName:#"DailypercentageViewController" bundle:nil];
//[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
//CGRect frame=CGRectMake(0,0,320,370);
[infoviewcontroller.view removeFromSuperview];
[self.view addSubview:rootview];
[UIView commitAnimations];
//[rearView setFrame:frame];
//[pieChart viewDidAppear:YES];
UIImage *buttonImage1 = [UIImage imageNamed:#"information_btn.png"];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:buttonImage1 forState:UIControlStateNormal];
button1.frame = CGRectMake(0, 0, 42, 32);
[button1 addTarget:self action:#selector(backPressed1) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
self.navigationItem.rightBarButtonItem = customBarItem1;
[customBarItem1 release];
//UILabel *label1 =[[[UILabel alloc]init]autorelease];
self.navigationItem.title=#"";
//label1.text = #"Home";
//self.title= #"Home";
}
`my problem is how i can remove this infoview and came back to the rootview from with in the button action from the infoview?

Animation in Uiscrollview with a button

I have tried the iCarousel, and add it to my project. I was able to put images. Now I'm trying to implement a method, that when the button was clicked the carousel will rotate.
I've tried this code:
- (IBAction) spinButton:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:carousel
cache:YES];
[spinButton removeFromSuperview];
[UIView commitAnimations];
}
But it seems to be that only the carousel view is animating.
Following two method to changes aries may be your problem being solve by this,
-(void)TappedButton:(UIButton *)sender{
UIView *view=[carousel itemViewAtIndex:sender.tag];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view cache:YES];
// [spinButton removeFromSuperview];
[UIView commitAnimations];
NSLog(#"%d",sender.tag);
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
//create new view if no view is available for recycling
if (view == nil)
{
view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"page.png"]] autorelease];
// label = [[[UILabel alloc] initWithFrame:view.bounds] autorelease];
// label.backgroundColor = [UIColor clearColor];
// label.textAlignment = UITextAlignmentCenter;
// label.font = [label.font fontWithSize:50];
//
// [view addSubview:label];
UIButton *buttonForDisplay = [[[UIButton alloc] initWithFrame: view.bounds] autorelease];
buttonForDisplay.backgroundColor=[UIColor clearColor];
[buttonForDisplay setTitle:[NSString stringWithFormat:#"%d",index] forState:UIControlStateNormal];
buttonForDisplay.titleLabel.font = [label.font fontWithSize:50];
[buttonForDisplay addTarget:self action:#selector(TappedButton:) forControlEvents:UIControlEventTouchUpInside];
[buttonForDisplay bringSubviewToFront:view];
buttonForDisplay.tag = index;
/// view.tag=index;
[view addSubview:buttonForDisplay];
}
else
{
label = [[view subviews] lastObject];
}
//set label
// label.text = [[items objectAtIndex:index] stringValue];
return view;
}
EDIT
This method thru you can catch the reference of the carousel view.
- (UIView *)itemViewAtIndex:(NSInteger)index
Edit with Animation
UIViewController *nextViewController = [[UIViewController alloc] autorelease];
nextViewController.view.backgroundColor = [UIColor redColor];
CATransition *animation = [CATransition animation];
animation.duration = 3.5;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFromTop;
[self.navigationController pushViewController:nextViewController animated:NO];
[self.navigationController.view.layer addAnimation:animation forKey:nil];

How to switch properly between UIViews

I would like to switch between UIViews in a correct way.
This is how I currently do it :
I trigger a method when clicking on a button
[myButton addTarget:self
action:#selector(switchToMySecondView:)
forControlEvents:UIControlEventTouchUpInside];
In switchToMySecondView, I allocate my new UIViewController (mySecondViewController is a attribute of the current class) :
MySecondViewController* mySecondView = [[MySecondViewController alloc] initWithNibName:#"SecondViewXib" bundle:nil];
self.mySecondViewController = mySecondView;
[mySecondView release];
Add some stuff in mySecondViewController...
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 50)];
myLabel.text = [aGlobalArray objectAtIndex:[sender tag]];
[mySecondViewController.view addSubView:myLabel];
Next I display it, using a UIAnimation :
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[mySecondViewController viewWillAppear:YES];
[self.view addSubview:mySecondViewController.view];
[mySecondViewController viewDidAppear:YES];
[UIView commitAnimations];
Finally, in my second view controller, I use the reverse method to switch back to my first view :
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.40];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view.superview cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
Ok. That works fine. I have no doubt there are memory leaks, but it's currently not my priority.
Here is my problem :
Each time I click on my button in the first view, I add a different UILabel to my second view (using the [sender tag] index in my aGlobalArray).
Each time this UILabel is added to my secondView, it overlays on the old UILabel, so I still can see the both UILabel.
UILabel is just an example. In my app I am also adding UIImages which overlay too.
I tried to write it in my secondView when switching back to my first view :
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view.superview cache:YES];
[self.view removeFromSuperview];
[self release];
[UIView commitAnimations];
And my app is suddenly stopped after 2 or 3 switch/switch back, sometimes without ny message in the console, sometimes with a message saying I am releasing something not allocated.
What am I doing wrong ?
Any help would be greatly appreciated !
You shouldn't be recreating the UILabel in the second view controller. You could either create the label in your MySecondViewController * class and expose it as a property. Then you'd simply do the following whenever you want to switch to the second view:
mySecondViewController.theLabel.text = [aGlobalArray objectAtIndex:[sender tag]];
Conversely, you could set a tag on the UILabel when you first create it and later retrieve the label using viewWithTag.
EDIT: When I say expose as a property, you'd create the label as normal in your interface:
#interface MySecondViewController : UIViewController {
UILabel *label;
}
#property (nonatomic, retain) UILabel *label;
The in the implementation file:
#implementation MySecondViewController
#synthesize label;
Then you'd create the label only once - i.e. in MySecondViewController ViewDidLoad or instead of your:
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 50)];
myLabel.text = [aGlobalArray objectAtIndex:[sender tag]];
[mySecondViewController.view addSubView:myLabel];
you could do:
if (mySecondViewController.label == nil) {
mySecondViewController.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 50)];
[mySecondViewController.view addSubView:self.mySecondViewController.label];
}
mySecondViewController.label.text = [aGlobalArray objectAtIndex:[sender tag]];

iOS - Problem to open frame(other view) in view

I have a view, and a button in this view. When I press this button I need open other view as a frame. Searching I found this post: iOS -- how do you control the size of a modal view controller?
I modify this and I doing this in a IBAction connected to a button:
View2Controller *screen = [[View2Controller alloc] initWithNibName:nil bundle:nil]; //Line add
UIView *myHalfView = [[UIView alloc] initWithFrame:screen.view.frame]; //Line modified by me
[self.view addSubview:myHalfView];
CGRect offScreenFrame = myHalfView.bounds;
offScreenFrame.origin = CGPointMake(0.0, CGRectGetMaxY(self.view.frame));
[UIView beginAnimations:nil context:nil];
myHalfView.center = CGPointMake(myHalfView.center.x, myHalfView.center.y - myHalfView.bounds.size.height);
[UIView commitAnimations];
[myHalfView release];
But when I press that button nothing happens. I've verified the code runs in debug it step by step.
Thanks.
Take a peak at this it's golden... read the comments in there as well.
http://humblecoder.blogspot.com/2009/04/iphone-tutorial-navigation-controller.html
Your View2Controller is initialized without a nibname?
I typically have a xib I built in Interface Builder in and do the following (have the view in the xib be whatever size you want by default if it doesn't change):
MyViewController* controller = [[MyViewController alloc]initWithNibName:#"MyViewController" bundle:nil];
[navigationController.view addSubview:controller.view];
//manipulate controller.view here
[controller release];
Thanks all for the replies. Finally I do this and works:
First in my IBAction:
View2Controller* modalViewController = [[[View2Controller alloc] initWithNibName:#"View2Controller" bundle:nil] autorelease];
[self.view addSubview:modalViewController.view];
Then in View2Controller.m in viewDidLoad:
[self.view setBackgroundColor:[UIColor clearColor]];
[UIView beginAnimations:nil context:nil];
[self.view setFrame:CGRectMake(0, 1024, 128, 600)];
[UIView setAnimationDuration:0.75f];
[self.view setFrame:CGRectMake(0, 404, 128, 600)];
[UIView commitAnimations];