Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;

}];
like image 990
johnbakers Avatar asked Jun 12 '11 23:06

johnbakers


2 Answers

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;

}];
like image 77
johnbakers Avatar answered Nov 17 '22 00:11

johnbakers


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);
}
like image 23
jdc Avatar answered Nov 17 '22 01:11

jdc