Reputation: 17169
I have this method which lets me translate the position of an object, animating its move in my iPhone app:
-(void)translatePositionForLabel:(UILabel *)label toFrame:(CGRect)newFrame
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
label.frame = newFrame;
[UIView commitAnimations];
}
You can see this works for UILabels
, but without having a duplicate of this method (just swapping the object for say UIButton
) is there anyway I can adapt this method so I can pass any object with a frame? Rather than needing an individual method for each object type.
Upvotes: 1
Views: 55
Reputation: 17015
You can also make a category for UIView
with methods for moving with animation:
UIView+Additions.h
@interface UIView (Additions)
- (void)setFrame:(CGRect)frame animated:(BOOL)animated;
- (void)translateToPoint:(CGPoint)point animated:(BOOL)animated;
@end
UIView+Additions.m
@implementation UIView (Additions)
- (void)setFrame:(CGRect)newFrame animated:(BOOL)animated {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.frame = newFrame;
[UIView commitAnimations];
}
- (void)translateToPoint:(CGPoint)point animated:(BOOL)animated {
CGRect newFrame = self.frame;
newFrame.origin = point;
[self setFrame:newFrame animated:animated];
}
@end
Now you can just call [button setFrame:newFrame animated:YES]
or [label setFrame:newFrame animated:YES]
.
Upvotes: 1
Reputation: 122401
Both UILabel
and UIButton
have a common ancestor in UIView
; try passing that in place of label
(you only appear to modify the label's frame
property, which is defined in UIView
).
Upvotes: 2