Animating UIView frame.origin - animation

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);
}

Related

UIImage Animation Reverting Back To Original Position

Im pretty new to coding but I'm starting to get the hang of the basics.
how to make an image stay in its new position after an animation?
Example:
I'm giving an animating object a random position, however, the animation causes the object not to animate at the random position, but instead animate at the position it was given in the view controller. This also happens when I animate a completly different object.
Code I used:
int Random1x;
int Random1y;
IBOutlet UIButton *Start;
IBOutlet UIImageview *Object2;
-(void)ObjectMoving;
-(void)Object2Animate;
-(IBAction)Start:(id)sender{
[self ObjectMoving];
[self Object2Animate];
}
-(void)Object2Animate {
Object2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed: #"3.png"],
[UIImage imageNamed: #"4.png"],
[UIImage imageNamed: #"1.png"], nil];
Object2.animationDuration = .5
[Object2 setanimationRepeatCount: 0]
[Object2 startAnimating];
}
-(void)ObjectMoving {
Random1y = arc4random() % 466;
Random1y = Random1y + 60;
Random1x = arc4random() % 288;
Object2.center = CGPointMake(Random1x, Random1y);
}
I'd greatly appreciate help, thank you!
If you go to your story board file and click on the View Controller and then file inspector you will see a box for Auto Layout
Post back if that worked.
If you do need to use auto layout then you would have to figure out a different way of moving the image.

stop animation using UIView in UILabel XCode

I have a doubt about to stop animation in UILabel,
I'm using a UIView to animate a UILabel, my code is similar to:
-(void)pickerRowTransformItem:(int)item{
//Get UILabel
UILabel *labelTem = (UILabel *)[self.picker viewWithTag:item];
if (labelTem){
[UIView animateWithDuration: 0.10
delay:0.0
options:UIViewAnimationTransitionNone | UIViewAnimationOptionCurveLinear
animations:^{
labelTem.transform = tr //Scale transform;
labelTem.textColor = color //Change color;
}
completion:^(BOOL finished){
}];
}
}
to stop animation, should I use?
-(void)pickerRowTransformItemStopAnimation:(int)item{
//Get UILabel
UILabel *labelTem = (UILabel *)[self.picker viewWithTag:item];
if (labelTem){
[labelTem.layer removeAllAnimations]; //stop animation??
}
}
or, which is the best way to stop a animation?
thanks!
I think the following article will benefit you. I believe this person solved the same question that you have. Try adding completion:NULL to your code instead of labelTem.layer removeAllAnimations.
How can I stop an UIViewAnimationOptionRepeat UIView Animation?

Mac OSX - Core Animation - how to animate something which is following my cursor?

So far I have only seen Core Animation has code that looks like this, where the parameters of where to animate to are set in the beginning.
- (void) startTopLeftImageViewAnimation{
/* Start from top left corner */
[self.xcodeImageView1 setFrame:CGRectMake(0.0f,0.0f, 100.0f, 100.0f)];
[self.xcodeImageView1 setAlpha:1.0f];
[UIView beginAnimations:#"xcodeImageView1Animation" context:(__bridge void *)self.xcodeImageView1];
/* 3 seconds animation */
[UIView setAnimationDuration:3.0f];
/* Receive animation delegates */ [UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector: #selector(imageViewDidStop:finished:context:)];
/* End at the bottom right corner */ [self.xcodeImageView1 setFrame:CGRectMake(220.0f,
350.0f, 100.0f,
[self.xcodeImageView1 setAlpha:0.0f];
[UIView commitAnimations];
}
However, I would like to make an animation in which the graphic follows my mouse cursor. This means that the animation is constantly receiving my mouse-pointer x-y values.
Is there a better alternative to doing "manual" animation like this, and calling drawRect whenever I shift the position of my mouse? Can I use Core Animation for this?
- (void)drawRect:(NSRect)rect
{
// Drawing code here.
[[NSColor whiteColor] set];
NSRectFill( rect );
[self drawCircleAtPoint:_circlePt];
}
- (void) drawCircleAtPoint:(CGPoint) point {
NSBezierPath* thePath = [NSBezierPath bezierPath];
NSRect nrect = NSMakeRect(point.x, point.y, 50, 50);
[thePath appendBezierPathWithOvalInRect:nrect];
[[NSColor blackColor] set];
[thePath stroke];
}
Thanks for any help regarding this.
First you would need the point where the mouse cursor is pointing to. The method you have to implement for this is
- (void)mouseMoved:(NSEvent *)theEvent
NSEvent has a method called
- (NSPoint)locationInWindow
which gives you, as you might guessed, the position of the cursor in the window.
Now you have to find the location within your view. This is done by NSView method
- (NSPoint)convertPoint:(NSPoint)aPoint fromView:(NSView *)aView
where the point comes from locationInWindow and the view is the view in which you want to perform the animation
e.g.
CGPoint mousePoint = [[[self window] contentView] convertPoint:[theEvent locationInWindow] fromView:self];
Then you can either set the position via the views frame or you can use the position property of the views layer.

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.

CGGradient isn't visible (not using interface builder) and UIButtons can't be triggered

I have created a view that contains a CGGradient:
// Bar ContextRef
CGRect bar = CGRectMake(0, screenHeight-staffAlignment, screenWidth, barWidth);
CGContextRef barContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(barContext);
CGContextClipToRect(barContext,bar);
// Bar GradientRef
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[16] = { 1.0,1.0,1.0,0.0, 0.0,0.0,0.0,1.0, 0.0,0.0,0.0,1.0, 1.0,1.0,1.0,0.0};
CGFloat locations[4] = {0.95,0.85,0.15,0.05};
size_t count = 4;
CGGradientRef gradientRef = CGGradientCreateWithColorComponents(colorSpace, components, locations, count);
// Draw Bar
CGPoint startPoint = {0.0,0.0};
CGPoint endPoint = {screenWidth,0.0};
CGContextDrawLinearGradient(barContext, gradientRef, startPoint, endPoint, 0);
CGContextRestoreGState(barContext);
This code is called in the drawRect method of the UIView. I then use a UIViewController to access the created view.
- (void)loadView {
MainPageView *mpView = [[MainPageView alloc] initWithFrame:[window bounds]];
[self setView:mpView];
[mpView release];
}
and displayed on the screen through the appDelegate:
mpViewController = [[MainPageViewController alloc] init];
[window addSubview:[mpViewController view]];
[window makeKeyAndVisible];
The UIView contains more objects, such as UIButtons, that are visible. I am assuming because they are added as a subview. But I can't work out how to add the CGGradient as a subview? Does it need to be? Is there another reason CGGradient is not visible?
I also don't get the functionality on the UIButtons. I guess that is because of where I have added the UIButtons to the view. Do the buttons need to be added in the UIViewController or the appDelegate to have functionality. Sorry to ask what would seem like simple questions but I am trying to accomplish the programming without the Interface Builder and material on that is scarce. If anyone could point me in the right direction on both these problems I would really appreciate it.
Thanks!
The functionality on the buttons was lost because the frame was too large but the buttons were still visible because the background was clearColor

Resources