I was wondering how to add a scrolling menu to my app like tiny wings or angry birds. In tiny wings, the background moves from right开发者_开发知识库 to left and I was wondering how they accomplished that. Thanks in advance.
Not familiar with either app (yes, a gaming Ludite), but if you make a view wider than the screen you can move it's location/center/frame/transform/etc. to make it move, you can animate that action too.
if you make the first 320 points (we measure in points, not pixels) of the width of the view equal to the last 320 points, you can jump the view's location when it reaches the end and keep going for ever.
Edit (example code):
- (void)animateBannerLocation {
UIView *view = _bannerView; // _bannerView.frame = CGRectMake(0,0,1000 ish,40);
CGRect startRect = view.frame;
CGRect destinationRect = view.frame;
// assuming superview is width of screen
destinationRect.origin.x = CGRectGetWidth(view.frame) - CGRectGetWidth(view.superview.frame);
[UIView animateWithDuration:6.0 // time in seconds
animations:^(void) {
view.frame = destinationRect;
} completion:^(BOOL finished) {
/** if you want it to scroll forever:
view.frame = startRect;
[self animateBannerLocation];
**/
}];
}
untested
Edit #2 also untested
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *_bannerView; /// - an ivar
// not the best way to load an image you only use in one place:
UIImage *image = [UIImage imageNamed:@"SomeBigBannerImage1000x40ish.png"];
_bannerView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:_bannerView];
}
Edit #3
Reviewed your code. You should pay attention to the compiler warnings.
- (void)animateBannerLocation {
UIView *view = _bannerView; // _bannerView.frame = CGRectMake(0,0,1000 ish,40);
CGRect startRect = view.frame;
CGRect destinationRect = view.frame;
// assuming superview is width of screen
destinationRect.origin.x = - (CGRectGetWidth(view.frame) - CGRectGetWidth(view.superview.frame));
[UIView animateWithDuration:6.0
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^(void) {
view.frame = destinationRect;
} completion:^(BOOL finished) {
view.frame = startRect;
if (stopAnimation == NO)
[self animateBannerLocation];
}];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self animateBannerLocation];
}
精彩评论