Here is what I currently have: http://cl.ly/PIFB
And the code behind it:
UIView *smallView = [[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:recognizer.view.tag inSection:0]] contentView];
CGRect initialSize = smallView.frame;
CGRect finalSize = CGRectMake(150.0, 100.0, 660.0, 450.0);
UIView *largeView = [[UIView alloc] initWithFrame:initialSize];
[largeView setBackgroundColor:[UIColor blackColor]];
[largeView setClipsToBounds:NO];
[UIView transitionFromView:smallView toView:largeView duration:2 options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
[[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:recognizer.view.tag inSection:0]] contentView].frame = finalSize;
[largeView setFrame:finalSize];
[smallView removeFromSuperview];
[self addSubview:largeView];
[UIView animateWithDuration:2 animations:^{
smallView.frame = finalSize;
[largeView setFrame:finalSize];
}];
}];
Basically I have a UITableView of views. When one of those views is tapped I would like to scale and flip it at the same time so that it shows a new larger view in the center of the screen. Currently it flips the smaller view and eventually shows the larger view at the right size but it doesn't transition smoothly.
In the code above smallView is the currently tapped cells content view. largeView is the new view that will eventually be developed to show more data relevant to the cell that was tapped. Right now it is represented by the black view. I really would like the small cell to grow and flip at the same time so that it smoothly transitions into the larger view.
Your help would be greatly appreciated. My experience with objective c, cocoa touch, and performing animations is pretty limited.
Thanks so much.
I finally figured it out. See here: http://cl.ly/PQqP
It required me to do two separate UIView transitionWithView methods:
[_transitionView addSubview:smallView];
[smallView setFrame:CGRectMake(0, 0, _transitionView.frame.size.width, _transitionView.frame.size.height)];
[UIView transitionWithView:_transitionView duration:.5 options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionCurveEaseOut animations:^(){
[UIView transitionWithView:cellView duration:.25 options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionCurveEaseIn animations:^(){
[cellView setFrame:smallViewOrigin];
} completion:NULL];
_transitionView.frame = finalSize;
smallView.frame = smallViewOrigin;
[_transitionView insertSubview:metricModalViewController.view belowSubview:smallView];
[smallView removeFromSuperview];
metricModalViewController.view.frame = metricModalEndSize;
} completion:^(BOOL finished) {
[_transitionView setFinalView:metricModalViewController.view];;
}];
It looks like you should use transitionWithView:duration:options:animations:completion: instead. Like this:
[UIView transitionWithView:self
duration:2
options:UIViewAnimationOptionTransitionFlipFromRight|UIViewAnimationOptionAllowAnimatedContent
animations:^(){
smallView.frame = finalSize;
[smallView removeFromSuperview];
[self addSubview:largeView];
largeView.frame = finalSize;
}
completion:NULL];
That might flip the self view, which might not look exactly right. If that's a problem, then add an extra view in your hierarchy which does nothing but contain smallView or largeView at the same size as them. Set the frame of that view in addition to those of the smallView and largeView. (Or just set the container view transform, or just set the container view frame and configure appropriate autoresizing options for the smallView and largeView.)
Maybe try this. Set up a view containerView that is a child of self and initially contains smallView. Make the frame of containerView what you initially had for smallView. Make the size of smallView the same as the size of containerView but the origin (0,0) so that smallView fills containerView entirely.
[UIView transitionWithView:containerView
duration:2
options:UIViewAnimationOptionTransitionFlipFromRight|UIViewAnimationOptionAllowAnimatedContent
animations:^(){
containerView.frame = finalSize;
smallView.frame.size = finalSize.size;
[smallView removeFromSuperview];
[containerView addSubview:largeView];
largeView.frame.size = finalSize.size;
}
completion:NULL];
I'm just guessing, again.
Related
May be this Question may be duplicate but i really need your help..
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
NewFeedDetailVC *nextViewController = [[NewFeedDetailVC alloc] initWithNibName:#"NewFeedDetailVC" bundle:[NSBundle mainBundle]];
nextViewController.originRect = imgFrame;
nextViewController.currentscrolloffset=aStylizedView.contentOffset.y;
[window addSubview:nextViewController.view];
[UIView animateWithDuration:0.5
animations:^{
nextViewController.view.frame = CGRectMake(0, 20, 320, ScreenHeight-20);
}
completion:^(BOOL finished){
}];
above is demo code i have tried to make zoom animation before push ..
imgFrame is image on which user will tap
When user click right top orange (four square) button ,and code as below
[UIView animateWithDuration:0.5
animations:^{
self.view.frame=self.originRect;
}
completion:^(BOOL finished){
[test removeFromSuperview];
[self.view removeFromSuperview];
}];
view should be pop out with zoom in animation to there parent frame but i am not able to perform smooth transition and image suppose to be same as parent frame and just black view is scaling to original frame, and another issue is you can observe animation is not clipping out in navigation control even if Its frame is clipping in original scroll view.
Thanks
I am try to create an animation in my app which would slide up a frame to my desired position. The frame would be having other buttons and text on it as well.
I was unable to use actionsheet for this, as it starts from the bottom most part of the window. What i want to create is shown in the picture below.
the frame I want to animate is the one with Capture mode: text.
Click on the button on the tabView I want this frame to slide up, starting from the top of the tabview. and again clicking the button should make the frame disappear.
So far my code looks like
-(IBAction)setting
{
NSLog(#"finalShare Button");
UIView *tempView;
CGRect tmpFrame;
tempView = [[[UIView alloc] initWithFrame:CGRectMake(0, 490, 320, 90)]
autorelease];
[tempView setBackgroundColor:[UIColor clearColor]];
[tempView setAlpha:.87];
tempView.hidden = YES;
[self.view addSubview:tempView];
tmpFrame = tempView.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
tmpFrame.origin.y=390;
self.view.frame=tmpFrame;
[UIView commitAnimations];
}
The problem I am facing here is that when I animate my frame to move, the main view moves along with it. Which is something I don't want it to do.
I am new to iPhone programming, please guide how can I fix this.
Or what would be a better way to do it.
Thanks
You don't need to make the view hidden, just add it as a subview after you set its frame to have y origin of 480. Also, don't use the old UIView animation methods; use blocks.
Here's an example of a view that starts with a y origin of 0. It gets positioned at negative y equal to its height, added as a subview then animated down to meet the keyboard coming up from the bottom of the screen.
// add to view while completely off-screen
CGRect unlockViewFrame = unlockView.frame;
unlockViewFrame.origin.y = -unlockViewFrame.size.height;
unlockView.frame = unlockViewFrame;
[self.view addSubview:unlockView];
unlockViewFrame.origin.y = 0.;
[UIView animateWithDuration:0.3 animations:^{
unlockView.frame = unlockViewFrame;
}];
Here's the same thing in reverse, using a completion block to remove the view.
unlockViewFrame.origin.y = -unlockViewFrame.size.height;
[UIView animateWithDuration:0.3 animations:^{
view.frame = unlockViewFrame;
} completion:^(BOOL completion) {
[view removeFromSuperview];
}];
Another thing I notice is that you are doing everything in code; consider doing it in Interface Builder. A smart man once told me that code you don't write can't have any bugs and he was right. It's easy to create a view in IB and instantiate it inyour code when you need it:
NSArray* nibViews;
nibViews = [[NSBundle mainBundle] loadNibNamed:#"UnlockView" owner:self options:nil];
UnlockView *unlockView = [nibViews objectAtIndex:0];
Also, consider using ARC. No more autorelease!
Your resetting frame of main view and not tempView
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
tmpFrame.origin.y=390;
tempView.frame=tmpFrame; //it should be tempView not self.view ie main view
[UIView commitAnimations];
I have a button on view1 and and I want to move it to view2 , but with animation from top. My view1 is on on the top half of the screen and view2 on the bottom half of the screen. So the superview changes as you can see. I have tried multiple approaches but the button just appears from the left or right but not from its origin. Any help is appreciated.
Make the superview not clip their contents by setting clipsToBounds to NO.
Then you animate the button's frame and when the animation is done you remove it from view1 and add it to view2.
[UIView animateWithDuration:0.2f
animations:^{
button.frame = newFrame;
}
completion:^(BOOL finished){
CGRect rect = [view1 convertRect:button.frame toView:view2];
[button removeFromSuperview];
[button setFrame:rect];
[view2 addSubView:button];
}];
I have accomplished this very result using the following code:
CGRect tabFrame = tab.frame;
CGRect viewFrame = [mainWindow convertRect:tabFrame toView:currentView];
tab.frame = viewFrame;
[tab removeFromSuperview];
[currentView addSubview:tab];
This code converts tab's frame from mainWindow into currentView. This works perfectly, aside from this bug. If you don't encounter that problem, or find out how to solve it I would really appreciate that.
All you need to do from that point is apply your normal animation.
I have a custom uiview that i am trying to update the frame size. I do it like this in a method once it is clicked. The method is called, and the frame value changes, however on the screen nothing happens!
if (!touched) {
GameBox *box = (GameBox *)[self.view viewWithTag:40];
[box setFrame:CGRectMake(20, 20, 100, 73)];
NSLog(#"%f", box.frame.origin.x);
markButton.enabled = NO;
guessButton.enabled = NO;
[self.view reloadInputViews];
NSLog(#"The action bar should now be hidden");
}
else if (touched) {
guessButton.enabled = YES;
markButton.enabled = YES;
[self.view reloadInputViews];
NSLog(#"The action bar should now be visible");
}
I guess you have hooked self.view to UIView in Interface Builder.
In order to change UIView frame, in Interface Builder go to File Inspector, and uncheck Use Autolayout, then in the code change the frame.
Hope this helps.
You need to set adjust the frame in the viewDidAppear (not viewDidLoad).
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
CGRect frame = self.tableView.frame;
frame.origin.y += 100.0;
frame.size.height -= 100.0;
self.tableView.frame = frame;
}
Apparently this has something to do with table views within navigation controllers.
It is because of Auto Layout, however you could do it after Auto Layout done its work or change constraints of it. If you want to do it after auto layout add following.
- (void)viewDidAppear:(BOOL)animated {
dispatch_async(dispatch_get_main_queue(), ^{
box.frame = CGRectMake(20, 20, 100, 73)];
});
[self.view reloadInputViews] is generally used for FirstResponders. I Don't have much of an idea about your customview, but I think for just settings the frame you can use:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
viewFrame.frame=frame;//Frame Defined by you
[UIView commitAnimations];
can give better solution if i get a look at your custom view.
I want to fade the whole screen (including navigation bar) to black when a user presses a button on a uinavigationcontroler, before showing a new view. (i don't want to push this new view, for various reasons).
How would I achieve this?
EDIT
Thanks to Mac and Eiko, I have figured it out. Here's the code I used. Not sure if it is optimal, but it does the trick.
// this is called from a programmatically constructed button.
// change (void) to (IBAction) if linking from IB.
- (void)fadeAndShow:(id)sender
{
// blackView is a #property which has been #synthesize-d
// do I really need to alloc and init this?
blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
blackView.alpha = 0.0;
[blackView setBackgroundColor:[UIColor blackColor]];
[self.navigationController.navigationBar.superview addSubview:blackView];
[UIView beginAnimations:#"fadeAway" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDidStopSelector:#selector(showNewScreen:finished:context:)];
blackView.alpha = 1.0;
[UIView commitAnimations];
}
-(void)showNewScreen:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{
// I guess you could fade in somewhere in the new view controller.
// don't know how to fade back in this view tho... viewDidAppear?
NewViewController *controller = [[NewViewController alloc] initWithNibName:#"NewView" bundle:nil];
[self.navigationController setNavigationBarHidden:YES];
controller.hidesBottomBarWhenPushed = YES;
[[self navigationController] pushViewController:controller animated:NO];
[blackView removeFromSuperview];
[controller release];
}
Off the top of my head (I haven't actually tested the following at all):
-(IBAction) buttonClicked:(id) sender
{
[UIView beginAnimations:#"myAnimation" context:nil];
[UIView setAnimationDuration:ANIMATION_DURATION];
blackView.alpha = 1.0;
[UIView commitAnimations];
}
Create a UIView in the navigationbar's superview (which I'm assuming is window-sized) that is the same size as the window.
Set that view's backgroundColor to [UIColor blackColor], and its alpha to 0.0.
In your button handler do something like the above (assuming your new UIView is blackView and ANIMATION_DURATION is your desired animation time in seconds).
Then, add your new view on top.
EDIT: too quick for me Eiko! Also, code at the top since the ordered list seems to screw around with the code formatting - sorry the answer reads a little odd.
You can add a black coloured UIView in screen size on top of your current view, and animate its alpha from 0 to 1. When the animation is done, add your new view. You can remove the black one then. Animate from 1 to 0 for the opposite effect - going from black to the content).