I'm trying to create sort of a slide-out slide-in motion for my views when the user presses the forward or backwards button.
I made the first view slide out just fine, but the second one just won't slide in.
Here's my code:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:0.1];
[UIView setAnimationDuration:0.7];
UIView *currentView = [[contentScrollView subviews] objectAtIndex:0];
currentView.center = CGPointMake(contentScrollView.frame.size.width+contentScrollView.frame.size.width/2, currentView.center.y);
UIView *newView = [[contentScrollView subviews] objectAtIndex:1];
newView.center = CGPointMake(newView.center.x+newView.frame.size.width, newView.center.y);
[UIView commitAnimations];
I don't really see what the problem is, it should work. But the currentView just slides out, and the newView is still out in the -x values, hidden from the user.
I already tried changing to transformations, but that didn't do anything.
What could cause this?
Try this code (tested):
Copy and paste it to see it in action and adjust the frames for your views to suit your needs.
UIView *viewOne = [[UIView alloc] initWithFrame:(CGRect){{0, 0}, 200, 200}]; //currentView
UIView *viewTwo = [[UIView alloc] initWithFrame:(CGRect){{200, 0}, 200, 200}]; //newView
viewOne.backgroundColor = [UIColor lightGrayColor];
viewTwo.backgroundColor = [UIColor darkGrayColor];
[self addSubview:viewOne];
[self addSubview:viewTwo];
[UIView animateWithDuration:5.0f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^
{
viewOne.frame = (CGRect){{300, 0}, 200, 200};
viewTwo.frame = (CGRect){{-100, 0}, 200, 200};
} completion:^(BOOL finished) {
}];
This is to show you how these two view sliding in and out.
You have to adjust the frames for your views accordingly to your needs.
Related
I'm using the following code to attempt to flip a UIView. It's a playing card. There's a container view, with two children, front and back. I've read in similar questions here that this should be the correct way to do it, but the actual flip itself is not being performed.
I'm intending for this to animate moving the card to the centre of the screen, and flip it from its back to front. The movement to centre is fine, but the flip never occurs.
If I change the View: argument to self, it flips the entire view controller, and if I leave it on the cardContainer, it does nothing at all.
Puzzled :(
UIView *cardContainer = [[UIView alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height + 20, [Card size].width, [Card size].height)];
[self.view addSubview:cardContainer];
Card *card = _playerOneCards[_playerOneNextCardIndex];
card.frame = CGRectMake(0, 0, [Card size].width, [Card size].height);
card.delegate = self;
[cardContainer addSubview:card];
CardBack *back = [[CardBack alloc] initWithFrame:CGRectMake(0, 0, [Card size].width, [Card size].height)];
[cardContainer addSubview:back];
[UIView transitionWithView:cardContainer
duration:duration
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
back.alpha = 0.0f;
cardContainer.center = self.center;
}
completion:nil];
Try this. The container view has to be placed in the hierarchy before the animation starts.
//Add container views and subviews
[CATransaction flush];
[UIView transitionWithView:cardContainer
duration:duration
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
back.alpha = 0.0f;
cardContainer.center = self.center;
}
completion:nil];
You can also wrap your function up in a dispatch_after call.
For more information check : http://andrewmarinov.com/working-with-uiviews-transition-animations/
I have a UITableViewController. When I receive a message I want to slide a UIView in from the top, display the message for a few seconds and then slide back up. So far I have this code:
-(void)receivedMessage
{
NSLog(#"you got a message!");
UIView *content = [[[NSBundle mainBundle] loadNibNamed:#"View" owner:self options:nil] objectAtIndex:0];
content.backgroundColor = [UIColor colorWithRed:190.0/225.0 green:86.8/225.0 blue:190.0/225.0 alpha:0.5];
content.frame=CGRectMake(0, -87, 320, 87);
[self.view addSubview:content];
self.headerView =content;
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerView.frame = CGRectMake(0, 5, 320,87);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerView.frame = CGRectMake(0, -507, 320,87);
} completion:^(BOOL finished) {
[content removeFromSuperview];
}];
}];
}
The animation works however if I pull down on the table I can see white space above the message box. I want the message to float and still allow me to scroll under it. How I would I do this? Do I have to modify - (void)scrollViewDidScroll:(UIScrollView *)scrollView?
thanks
Why are you setting self.headerView to content? You should just add the content view to self.view and animate it.
The headerView is part of the UITableView so that why when you scroll you see the white space.
I am having one UIButton on navigation bar as right bar button item.
I want to show one view when I click on the UIButton,
and that view should appear animating from right to left and stops to perform some operations for user.
when user is done from his work on the view he will click on same button and view will hide animating from right to left.
I tried to load view normally I get succeed in it,
when I apply animation
view animates from right to left and disappears.
I am new to this animation scenario.
So can anyone please guide me to animate view and show hide action on same button.
Here is the code that I tried,
-(IBAction)showView :(id)sender
{
CGRect screen = [[UIScreen mainScreen]applicationFrame];
UIView *quizInfoViewObj;
quizInfoViewObj = [[UIView alloc]init];
quizInfoViewObj.frame = CGRectMake(70, 0, 250, 480);
quizInfoViewObj.backgroundColor = [UIColor orangeColor];
[UIView beginAnimations: nil context: NULL];
[UIView setAnimationDelegate: self];
[UIView setAnimationDidStopSelector: #selector(pushAnimationDidStop:finished:context:)];
quizInfoViewObj.center = self.view.center;
quizInfoViewObj.center = CGPointMake(self.view.center.x - CGRectGetWidth(self.view.frame), self.view.center.y);
[UIView commitAnimations];
[self.view addSubview:quizInfoViewObj];
}
here how I want...
This code might help you.
-(IBAction)showView :(id)sender
{
UIButton *button = (UIButton *)sender;
if ([button isSelected] == NO) {
CGRect screen = [[UIScreen mainScreen]applicationFrame];
// UIView *quizInfoViewObj; // use this line in .h file variables declaration
quizInfoViewObj = [[UIView alloc]init];
quizInfoViewObj.frame = CGRectMake(self.view.frame.width, 0, 250, 480);
quizInfoViewObj.backgroundColor = [UIColor orangeColor];
[self.view addSubview:quizInfoViewObj];
[UIView beginAnimations: nil context: NULL];
quizInfoViewObj.frame = CGRectMake(70, 0, 250, 480);
[UIView commitAnimations];
}
else {
[UIView beginAnimations: nil context: NULL];
[UIView setAnimationDelegate: self];
[UIView setAnimationDidStopSelector: #selector(pushAnimationDidStop:finished:context:)];
// In pushAnimationDidStop:finished:context method remove quizInfoViewObj from superview
quizInfoViewObj.frame = CGRectMake(self.view.frame.width, 0, 250, 480);
[UIView commitAnimations];
}
[button setSelected: ![button isSelected]];
}
I have developed a font selection that utilizes UIPickerView and UIToolbar, which are both added on touching a UIButton.
But they just kind of pop in which looks sloppy.
is there a way to animate them?
here is the method that adds them (it's called on clicking the button) :
-(void)showPicker
{
[self.view addSubview:_fontPicker];
[self.view addSubview:pickerTB];
}
You can animate them in a couple of ways, but probably the easiest is to just fade them in.
Fade In
[someView setAlpha:0]
[self.view addSubview:someView];
[UIView beginAnimations:#"FadeIn" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:1];
[UIView commitAnimations];
Fade Out
float fadeDuration = 0.5;
[UIView beginAnimations:#"FadeOut" context:nil];
[UIView setAnimationDuration:fadeDuration ];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:0];
[UIView setAnimationDidStopSelector:#selector(removeView)];
[UIView commitAnimations];
-(void) removeView {
[someView removeFromSuperview];
}
Something as simple as that.
Depends on what type of animation you want to use. For example, this would look like dissolve.
Anyways look into UIView animateWithDuration...
pickerTB.alpha = _fontPicker.alpha = 0;
[self.view addSubview:_fontPicker];
[self.view addSubview:pickerTB];
[UIView animateWithDuration:1 animations:^{_fontPicker.alpha = 1; pickerTB.alpha = 1}];
You could use transforms or change frame origins to make the views fly in from positions outside the screen too. In that case, inside the animations block, specify the new on screen position. You can do this in two ways, change the frame with an updated origin or apply a CGAffineTransformMakeTranslation(dx,dy)
You need to add the subview with a frame value that is where you want the animation to begin from (off-screen?) then change the frame value inside your animation block. The frame will change over the duration, causing the appearance of movement. The view must already be a subview - I don't believe adding a subview is something you can animate, it makes no sense.
-(void) showPicker{
[self.view addSubview: _fontPicker];
_fontPicker.frame = CGRectMake(0, 0, 120, 40);// somewhere offscreen, in the direction you want it to appear from
[UIView animateWithDuration:10.0
animations:^{
geopointView.frame = // its final location
}];
}
Initially set your picker view frame and tool bar frame like this one..
-(void)viewDidLoad
{
[super viewDidLoad];
pickerTB.frame = CGRectMake(0,568,320,44);
fontPicker.frame = CGRectMake(0, 568, 320, 216);
}
-(void)showPicker
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.30];
if(isiPhone5)
{
pickerTB.frame = CGRectMake(0,288,320,44);
fontPicker.frame = CGRectMake(0, 332, 320, 216);
}else{
pickerTB.frame = CGRectMake(0,200,320,44);
fontPicker.frame = CGRectMake(0,244, 320, 216);
}
[UIView commitAnimations];
}
I'm new iphone developer i have problem uiview animation for ipad landscape mode.i have two view view1(like a split view) and view2(covered remaining window).when i touch move right to left view2 it will be overriding the view1 with moving animation.at same time when i touch move left to right view2 come and fit the old position.please share your ideas
With Regards,
Rajesh.
Hi all i found the answer finally,
- (void)viewDidLoad
{
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[recognizer setNumberOfTouchesRequired:1];
[secondView addGestureRecognizer:recognizer];
[recognizer release];
UISwipeGestureRecognizer *recognizerleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
[recognizerleft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[recognizerleft setNumberOfTouchesRequired:1];
[secondView addGestureRecognizer:recognizerleft];
[recognizerleft release];
[self.view addSubview:secondView];
[super viewDidLoad];
}
//To set the frame position of the view
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
NSLog(#"rightSwipeHandle");
[UIView beginAnimations:#"viewanimations" context:nil];
[UIView setAnimationDuration:0.8];
[UIView setAnimationDelegate:self];
secondView.frame=CGRectMake(360, 0, 660, 768);
[UIView commitAnimations];
}
//To set the frame position of the view
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
NSLog(#"leftSwipeHandle");
[UIView beginAnimations:#"viewanimations" context:nil];
[UIView setAnimationDuration:0.8];
[UIView setAnimationDelegate:self];
secondView.frame=CGRectMake(200, 0, 858, 768);
[UIView commitAnimations];
}
if i understand correctly, you want a sliding animation between 2 views. if so:
Make a ViewController that will hold these two views. inside this view controller's .m file define a transition method that contains the following and gets called by your buttons/other action triggers:
CGFloat windowWidth = self.mainView.frame.size.width;
CGFloat windowHeight = self.mainView.frame.size.height;
CGRect offScreenLeft = CGRectMake(-1*windowWidth, 0.0, windowWidth, windowHeight);
CGRect onScreen = self.mainView.frame;
CGRect offScreenRight = CGRectMake(windowWidth, 0.0, windowWidth, windowHeight);
if (direction == rightToLeft)
{
rightView.frame = offScreenRight;
[self.view addSubview:rightView];
[UIView animateWithDuration:0.65
animations:^{
leftView.frame = offScreenLeft;
rightView.frame = onScreen;
}
completion:^(BOOL finished){
[leftView removeFromSuperview];
}];
}else if (direction == leftToRight){
self.leftViewController.view.frame = offScreenLeft;
[UIView animateWithDuration:0.65
animations:^{
rightView.frame = offScreenRight;
leftView.frame = onScreen;
}
completion:^(BOOL finished){
[rightView removeFromSuperview];
}];
}
}
In general, make sure you are adding your subview to the view currently on screen, and make sure you set the appropriate bounds. Check that your view is not nil:
if(myView){
// not nil. thats good
}else {
// nil. this is bad. make sure myView is being initialized properly (if at all)
}
Finally, make sure neither the opacity nor the alpha property on your subview is set to 0 (you'll want these at 1 if you want it to show up completely and between 0 and 1 for a transparency effect).