动画UIView原点仅在首次尝试后起作用

时间:2012-10-02 22:09:45

标签: objective-c ios animation uiview

我有一个带有一些标签的UIView,我在我的故事板中构建了一个标签,我想在点击地图注释时制作动画。我的一切都很好,除了动画有点偏。第一次单击注释时,帧位置不会设置动画。但是,如果我取消选择然后重新选择注释,那么从那时起动画就会很好。我也设置了背景颜色的动画,这似乎在第一个选择上工作得很好,所以不管它是什么特殊的框架属性,也许是因为它没有在加载时适当设置?我再次在故事板中做了所有这些,所以我不知道我错过了什么。

我的标签/视图代码(所有内容都在我的故事板中,并使用参考出口连接到正确的IBOutlets)。 file.h:

@property (weak, nonatomic) IBOutlet UIView *stationinfo;
@property (weak, nonatomic) IBOutlet UILabel *stationname;
@property (weak, nonatomic) IBOutlet UILabel *stationsong;
@property (weak, nonatomic) IBOutlet UILabel *stationartist;

file.m:

@synthesize stationinfo;
@synthesize stationname;
@synthesize stationsong;
@synthesize stationartist;

请在下面找到回调代码:

- (void) mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    //NSLog(@"Selected ");
    self.stationname.text = ((userStation *)view.annotation).name;
    self.stationsong.text = ((userStation *)view.annotation).song;
    self.stationartist.text = ((userStation *)view.annotation).artist;
    [UIView
     animateWithDuration:0.25
     delay: 0.0
     options: UIViewAnimationCurveEaseInOut
     animations:^ {
        CGRect anim = self.stationinfo.frame;
        anim.origin.y = 0.0;
        self.stationinfo.frame = anim;
        self.stationinfo.backgroundColor = [UIColor redColor];
    }
     completion:^(BOOL finished){
         NSLog(@"DONE!");
     }];
}

- (void) mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
    //NSLog(@"Deselected");
    [UIView
     animateWithDuration:0.25
     delay: 0.0
     options: UIViewAnimationCurveEaseInOut
     animations:^ {
         CGRect anim = self.stationinfo.frame;
         anim.origin.y = -100.0;
         self.stationinfo.frame = anim;
         self.stationinfo.backgroundColor = [UIColor greenColor];
     }
     completion:^(BOOL finished){
         NSLog(@"DONE!");
     }];
}

非常感谢您的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

编辑:虽然以下工作过,但我发现事实上已经不再需要了!我只能想象有一天,当我在我的布局中应用适当的约束以确保它在新的屏幕尺寸上看起来很好时,我无意中修复了调整我的故事板的错误。

我找到的工作是在完整的功能中再次执行动画。虽然不优雅似乎有效(因为只有第一个动画调用不起作用,第二个动画完全再次执行)。请注意,这将导致延迟,因为第一次尝试必须先完成。我猜测帧的某些属性是由第一个动画初始化的,这个动画需要为该位置设置动画。

[UIView
         animateWithDuration:0.25
         delay: 0.0
         options: UIViewAnimationCurveEaseInOut
         animations:^ {
            CGRect anim = self.stationinfo.frame;
            anim.origin.y = 0.0;
            self.stationinfo.frame = anim;
        }
        completion:^(BOOL finished){
            CGRect anim = self.stationinfo.frame;
            anim.origin.y = 0.0;
            self.stationinfo.frame = anim;
        }];

我猜或者可以在主要动画之前发出中性动画,看看它是否具有相同的效果。