I have a UIView in my viewcontroller. It has frame (34,250,253,193) and it is the center horizontally. I want to zoom in and zoom out this UIView. With Zoom in, it works fine with center. However with Zoom out, it works incorrect. Currently,the view zoom out from right to left. I don't want this. I want it zoom out with center.
This is my code
-(void)zoomin:(UIView*)view {
view.transform = CGAffineTransformMakeScale(0.1,0.1);
[UIView animateWithDuration:2.0 animations: ^{
view.transform = CGAffineTransformMakeScale(1,1);
}];
}
-(void)zoomout:(UIView*)view {
view.transform = CGAffineTransformMakeScale(1,1);
[UIView animateWithDuration:2.0 animations: ^{
view.transform = CGAffineTransformMakeScale(0.1,0.1);
}];
}
The problem is that your code, as far as you have shown it, works perfectly:
It therefore follows logically that if there's a problem, it is caused by something you have not shown. But since you have not shown it, there is no way of knowing what it is (aside from guessing).
Related
Im using Xcode, and when i tap/drag on my imageView at the bottom of the screen, I want it to be brought up and reveal all of it on screen similiar to the way Spotify does it when you click the banner at the bottom. Any ideas?
I recommend using spring animations if you want a nice animation like this:
func buttonTapped(sender: UIButton!) { //or in an IBAction
let duration: NSTimeInterval = 0.75
let damping: CGFloat = 1
let velocity: CGFloat = 0.5
UIView.animateWithDuration(duration, delay: 0.5, usingSpringWithDamping: damping, initialSpringVelocity: velocity, options: .CurveLinear, animations: {
self.myView.center.y = self.view.frame.height/2
}, completion: nil)
}
Sorry for Swift code, if you're not able to translate it then let me know :)
Edit
For Objective-C the code would look something like this:
[UIView animateWithDuration:0.75, delay:0, usingSpringWithDamping:1, initialSpringVelocity:0.5, options:UIViewAnimationOptionsCurveLinear, animations:^{
//Animations
} completion:^(BOOL finished) {
//Completion Block
}];
I actually figured it out. I made a button and included this code to trigger the animation:
self.moveX.constant = 200;
[UIView animateWithDuration:2.0f animations:^{
imageView.frame = CGRectMake(0.0f, 200.0f, imageView.frame.size.width,imageView.frame.size.height);
}];
I am working on a Mac app. I am trying to do a simple animation which makes an NSButton move down. The animation works really nicely, but when i do it, the background colour of my NSButton disappears for some reason. Here is my code:
// Tell the view to create a backing layer.
additionButton.wantsLayer = YES;
// Set the layer redraw policy. This would be better done in
// the initialization method of a NSView subclass instead of here.
additionButton.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
context.duration = 1.0f;
additionButton.animator.frame = CGRectOffset(additionButton.frame, 0.0, -20.0);
//additionButton.frame = CGRectOffset(additionButton.frame, 0.0, -20.0);
} completionHandler:nil];
Button move down animation:
Button after move down animation:
Update 1
Just to make it clear, I am not using a background image in my buttons. I am using a background NSColor which I set in the viewDidLoad method like so:
[[additionButton cell] setBackgroundColor:[NSColor colorWithRed:(100/255.0) green:(43/255.0) blue:(22/255.0) alpha:1.0]];
I presume this is an AppKit bug. There are a couple of ways you can work around it.
Workaround 1:
Don't use layers. The button you're animating seems to be small, and you might be able to get away with using a non layer-backed animation and still have it look decent. The button will redraw during each step of the animation, but it will animate correctly. That means this is really all you have to do:
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
additionButton.animator.frame = CGRectOffset(additionButton.frame, 0, -20);
} completionHandler:nil];
Workaround 2:
Set the background color on the layer.
additionButton.wantsLayer = YES;
additionButton.layer.backgroundColor = NSColor.redColor.CGColor;
additionButton.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
additionButton.animator.frame = CGRectOffset(additionButton.frame, 0, -20);
} completionHandler:nil];
Workaround 3:
Subclass NSButtonCell, and implement -drawBezelWithFrame:inView:, drawing your background color there. Keep in mind that the parent view containing the button should be layer-backed, otherwise the button will still redraw on every step.
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];
}
];
}
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.
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;
}];