CGRectMake Revert back to original position? - xcode

Im moving a UIView from its original position as laid out on my XIB/Storyboard using the following:
-(void)myMovingViewAction{
[UIView animateWithDuration:.15
animations:^{
[theView setFrame:CGRectMake(0, 40, theView.frame.size.width, theView.frame.size.height)];
}
];
}
Problem i have is getting the UIView back to its original postion as laid out on my Storyboard UIViewController/XIB.
I know i could use the same code as above with the original co-ordinates in it, but these co-ordinates are different on different devices. in this example, Constraints are keeping my UIView hugged against the bottom of my UIViewController in its original position.
Is there a way i can send this view back to 'default' position?
Thanks in advance! :)

create an instance var theFrame of type CGRect, set it to theView.frame on viewDidAppear. when you want to move it to back to original position, you can do
[UIView animateWithDuration:.15
animations:^{
[theView setFrame:theFrame];
}
];
}

Related

NSView won't draw through bezierpath

Dear fellow Cocoa programmers,
What I'd like to accomplish:
I have a checkbox, a popUpButton(which is hidden) and a NSView on my canvas.
If myCheckbox is checked -> show the popUpButton and draw a line through bezierPath on the NSView.
if myCheckbox is UNchecked -> Hide the popUpButton again and "undraw" the path
The code:
- (IBAction)isChecked:(id)sender {
//if myChekcbox is checked, show the pop up button
if ([sender state]==NSOnState) {
NSLog(#"Checked");
[myPopUp setHidden:NO];
}
else
{
//if the checkbox is unchecked, hide the popupbutton
[myPopUp setHidden:YES];
NSLog(#"Unchecked");
}
//reload my drawrect method (reload the view)
[self setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)dirtyRect
{
//if the checkedbutton is checked, draw the line
if ([myCheckbox state]==NSOnState)
{
NSBezierPath *myPath = [NSBezierPath bezierPath];
[myPath moveToPoint:NSMakePoint(10, 20)];
[myPath lineToPoint:NSMakePoint(50, 20)];
[myPath setLineWidth:2];
[myPath stroke];
}
}
The problem:
if checked state = NSOnState the popUpButton is visible but the line just won't draw and I wonder why... I personally think it's a connection(s) problem.
I uploaded the project file (it's rather small-35kb) here:Drawing.zip
Globally:
I've read the NSView documentation and it's saying there is only one way to draw to a view and it's through the drawRect method. Is this actually true? Also is this a descent way to draw to a view? (if function in the view and setNeedsDisplay:YES in the method)
thanks in advance,
Ben
You will need to get an NSColor instance and then call setStroke on it to set a current stroke color. It does not know which color to use to stroke the path at the start of drawRect:, so you have to tell it.

Removing superview is already done before animation

I have an app where a movie view is a kind of loading screen, and it's on top of my root controller, splitViewController. When the movie has finished, i want to remove it from the superview, animated. I'm using this code now, where mpmctr my movie controller is:
[UIView beginAnimations:#"blablablab" context:NULL];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:splitViewController.view.superview cache:NO];
[UIView setAnimationDuration:.5];
[mpMCtr.view removeFromSuperview];
[UIView commitAnimations];
When this code runs, mpmctr removes itself from the superview but not animated. This is happening when the splitviewcontroller is already on the screen.
Im using this code for putting mpmctr on the view in the delegate method didfinishlaunching.
[window addSubview:splitViewController.view];
[splitViewController.view addSubview:mpMCtr.view];
I hope that you guys can help me with this problem,
Thanks in advance.
A UIView animation can't animate removal from superview, but you could for example animate its alpha down to zero, then you could do this to remove the view after your animation has completed.
[UIView setAnimationDidStopSelector:#selector(removeMyView)];
- (void) removeMyView
{
[mpMCtr.view removeFromSuperview];
}
Despite what MDT says, you can actually animate the removal of a view with UIView animation. You just have to use the block-based API that was introduced in iOS 4.
This is the exact sample code from the Apples documentation for transitionWithView:duration:options:animations:completion:
[UIView transitionWithView:containerView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[fromView removeFromSuperview];
[containerView addSubview:toView]; }
completion:NULL];
It will flip from left, removing fromView and adding toView to containerView (the view that they are added removed from).

how to do animated transition between different UIViewControllers within a UITabBarController?

I have a UITabBarController,displaying 4 tabs.
I want to add an animated UIViewController transition when user swiping screen to switch tab. (- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController method only work for select tab directly). transition will be in easeinout style.
I tried following codes but not work. only coming UIViewController moves, going UIViewController doesn't show at all.(I printed all UIViewControllers' frame data, all these 4 UIViewControllers are {0,0},{320,480} )
// Get views. controllerIndex is passed.
UIViewController *fromVC = [_tabBarController.viewControllers objectAtIndex:startIndex];
UIViewController *toVC = [_tabBarController.viewControllers objectAtIndex:to];
UIView *fromView = fromVC.view;
UIView *toView = toVC.view;
MWLog(#"from view is %#",fromView);
int direct = startIndex - to;
// calculate move direction.
if (direct < 0) {
CGRect outFrame = fromView.frame;
outFrame.origin.x = -320; // expect fromView will move to {-320,0}, out of screen.
MWLog(#"fromView's frame is %#", NSStringFromCGRect(fromView.frame));
CGRect inFrame = toView.frame;
inFrame.origin.x = 0;
MWLog(#"toView's frame is %#", NSStringFromCGRect(toView.frame));
[UIView beginAnimations:#"moveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5f];
toView.frame = inFrame;
fromView.frame = outFrame;
[UIView commitAnimations];
}else
{
// reverse moving. ignored....
}
Could you tell me what's wrong and how to do it correctly? thanks in advance!
finally I found the solution.
use CATransition animation, type push, type left or right depending on direction(by calculating tabbarcontroller's selectedIndex property). then, add this animation to tabbarcontroller's container view.
This view's reference can be got by enumerate all UIViews in [tabbarcontroller subviews] array. Actually, if no custom UIView, tabbarcontroller contains 2 subviews, one is UITabBar, the other is the container view, UITransitionView. Add animation on this view, you can enable page transition animation for different content screen.

Animating UIView frame.origin

I have seen some sample code online regarding a simple UIView slide-in or slide-out. It makes sense to have a UIView start with its coordinates off the screen (negative) and then the animation parameter simply changes the UIView's frame. So first question is: does this provide the most obvious method?
However, in my code, the frame origin is not assignable. I get the Xcode error for that line that says as much.
I simply want to do something like this inside an animation:
self.viewPop.frame.origin.y=100;
Any help is appreciated.
UPDATE
I have solved this problem. The technique I discovered is to assign your view's frame to another CGRect, and make changes to that CGRect, then assign it back to the frame:
CGRect frame = self.moveView.frame;
frame.origin.x = newvalue.
[UIView animateWithDuration:2.0 animations:^{
self.moveView.frame = frame;
}];
I have solved this problem. The technique I discovered is to assign your view's frame to another CGRect, and make changes to that CGRect, then assign it back to the frame:
CGRect frame = self.moveView.frame;
frame.origin.x = newvalue.
[UIView animateWithDuration:2.0 animations:^{
self.moveView.frame = frame;
}];
FWIW, it's nice to have a UIView category around for this type of thing:
#interface UIView (JCUtilities)
#property (nonatomic, assign) CGFloat frameX;
-(CGFloat)frameX;
-(void)setFrameX:(CGFloat)newX;
#end
#implementation UIView (JCUtilities)
- (CGFloat)frameX {
return self.frame.origin.x;
}
- (void)setFrameX:(CGFloat)newX {
self.frame = CGRectMake(newX, self.frame.origin.y,
self.frame.size.width, self.frame.size.height);
}

iOS - animate movement of a label or image

How can I animate the movement of a label or image? I would just like to make a slow transition from one location on the screen to another (nothing fancy).
For ios4 and later you should not use beginAnimations:context and commitAnimations, as these are discouraged in the documentation.
Instead you should use one of the block-based methods.
The above example would then look like this:
[UIView animateWithDuration:0.3 animations:^{ // animate the following:
myLabel.frame = newRect; // move to new location
}];
You're looking for the -beginAnimations:context: and -commitAnimations methods on UIView.
In a nutshell, you do something like:
[UIView beginAnimations:nil context:NULL]; // animate the following:
myLabel.frame = newRect; // move to new location
[UIView setAnimationDuration:0.3];
[UIView commitAnimations];
Here is an example with a UILabel - the animation slides the label from the left in 0.3 seconds.
// Save the original configuration.
CGRect initialFrame = label.frame;
// Displace the label so it's hidden outside of the screen before animation starts.
CGRect displacedFrame = initialFrame;
displacedFrame.origin.x = -100;
label.frame = displacedFrame;
// Restore label's initial position during animation.
[UIView animateWithDuration:0.3 animations:^{
label.frame = initialFrame;
}];

Resources