2016-02-23 134 views
0

我有一个UIImageView,使用对齐中心X和居中Y约束来放置在屏幕中央。我希望图像随着动画移动到屏幕顶部,保持容器中的水平对齐并且当我按下按钮时从顶部具有20的空间。以编程方式移除Align Center Y约束,并添加顶部空间约束是一个很好的方法吗?带有约束的UIImageView的Ios动画

+0

试试这个http://stackoverflow.com/a/12664093 – deadbeef

回答

0

动态计算UIScreen类和视图高度的常量。

@interface ViewController : UIViewController 

@property (nonatomic, weak) IBOutlet NSLayoutConstraint *centerYConstraint; 
@property (nonatomic, weak) IBOutlet UIView *myView; 

- (IBAction)moveView:(id)sender; 

@end 


- (IBAction)moveView:(id)sender 
{ 
    self.centerYConstraint.constant = 20.0 + (self.myView.frame.size.height * 0.5) - ([UIScreen mainScreen].bounds.size.height * 0.5); 
    [UIView animateWithDuration:1.0 animations:^{ 
     [self.view layoutIfNeeded]; 
    }]; 
} 

你可以在这里下载代码(http://cl.ly/3l1B0k1L0h1C

+0

它的工作原理就像一个魅力!非常感谢你! – user3734693