iphone - 让UIImageView重复移动

时间:2013-07-22 21:15:38

标签: iphone objective-c function uiimageview viewdidload

背景:我使用的是xcode 3.1.4和iphone模拟器3.1(我知道它已经过时了,请不要对此发表评论)。

目标:我试图获取一个视图加载后创建的UIImageView,以便不断向下移动。创建UIImageView工作正常,但移动功能不起作用。什么都没发生。我使用的是NSTimer。这是我在视图controller.m文件中的代码:

-(void)viewDidLoad{
    UIImageView *six = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Meteor.png"]];
    CGRect rectSix = CGRectMake(arc4random() % (250), arc4random() % (1), 35, 35);
    [six setFrame:rectSix];
    [self.view addSubview:six];
    [self moveMeteor:six];
    [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(moveMeteor:) userInfo:six repeats:YES];

}

-(void)moveMeteor:(UIImageView *)t{

    t.center=CGPointMake(t.center.x, t.center.y + 1); 


}

当然,我在视图controller.h文件中声明了我的moveMeteor函数。输入:

-(void)moveMeteor:(UIImageView *)t;

关于问题是什么以及解决方案的任何想法?

2 个答案:

答案 0 :(得分:0)

您的方法实际上没有获得正确的参数,您将NSTimer作为参数。所以反而

-(void)moveMeteor:(UIImageView *)t

方法应该是这样的

-(void)moveMeteor:(NSTimer *)timer

并且您不能将NSTimer视为UIImageView。 :)

*修改

我建议你做一些像

这样的事情
-(void)viewDidLoad{
    UIImageView *six = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Meteor.png"]];
    CGRect rectSix = CGRectMake(arc4random() % (250), arc4random() % (250), 35, 35);
    [six setFrame:rectSix];
    [self.view addSubview:six];
    [self moveMeteor:six];
    [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(moveMeteor:) userInfo:six repeats:YES];    
}

并将您的方法更新为此

- (void)moveMeteor:(NSTimer *)timer {
    UIImageView *six = timer.userInfo;
    six.center=CGPointMake(six.center.x, six.center.y + 1); 
}

仅供参考,我将字典中的UIImageView作为userInfo传递。

答案 1 :(得分:0)

或者你可以做这样的事情,它利用了UIView的animateWithDuration并且应该看起来好多了。无需计时器。

// in your interface declaration...
@property (nonatomic, assign) BOOL meteorShouldAnimate;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

// in your implementation...
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.meteorShouldAnimate = YES;
    [self moveMeteorWithAnimationOptions:UIViewAnimationOptionCurveEaseIn]; // ease in to the animation initially
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.meteorShouldAnimate = NO;
}


- (void)moveMeteorWithAnimationOptions: (UIViewAnimationOptions) options {
    __weak MyViewController * weakSelf = self;
    if (self.meteorShouldAnimate) {
        [UIView animateWithDuration:0.2 delay:0.0 options:options animations:^{
            self.imageView.transform = CGAffineTransformMakeTranslation(20.0, 20.0); // or whatever x and y values you choose
        } completion:^(BOOL finished) {
            MyViewController * strongSelf = weakSelf; // in case our view controller is destroyed before our completion handler is called
            if (strongSelf) {
                UIViewAnimationOptions nextOptions = strongSelf.meteorShouldAnimate ? UIViewAnimationOptionCurveLinear : UIViewAnimationOptionCurveEaseOut; // linear if we're continuing to animate, ease out if we're stopping
                [strongSelf moveMeteorWithAnimationOptions:nextOptions];
            }
        }];
    }
}