iOS:Masonry+UIView动画
iOS:Masonry+UIView animation
我正在尝试使用 [UIView animationWithDuration]
方法通过约束为 UIView 设置动画。
我非常熟悉使用 UIView.frame 的动画,这非常简单。
在 init 方法中我添加了那些子视图
-(instancetype)init
{
if (self = [super init]) {
[self addSubview:self.bg_view];
[self.bg_view addSubview:self.content];
[self.bg_view addSubview:self.titleView];
[self.bg_view addSubview:self.reward];
}
return self;
}
和Masonry文件中显示的一样,约束设置在
-(void)updateConstraints
{
[self makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo([UIApplication sharedApplication].keyWindow);
}];
[self.bg_view makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
[self.titleView makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.bg_view.centerX);
make.top.equalTo(self.bg_view.top).with.offset(150);
}];
[self.content makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(self.bg_view).with.offset(-k_Margin*2);
make.bottom.equalTo(self.reward.top).with.offset(-20);
make.centerX.equalTo(self.bg_view);
make.top.equalTo(self.titleView.bottom).with.offset(20);
}];
[self.reward makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.bg_view.centerX);
make.top.equalTo(self.content.bottom).with.offset(20);
make.bottom.equalTo(self.bg_view.bottom).with.offset(-40);
}];
[super updateConstraints];
}
并且在show方法中
-(void)show
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
[self setNeedsUpdateConstraints];
[UIView animateWithDuration:5.0f animations:^{
/* I have tried variety ways to get the uiview come into the
screen from the top*/
}];
}
我在下面发布了一些代码来帮助您实现此目的,我使用了 viewcontroller,如果您愿意,您可以像 Masonry 示例中那样使用 UIView。我在我的视图上放置了一个按钮并将其连接到我的 'show' 方法。
看看 Masonry 项目中的 iOS 示例,它们很有帮助。
@interface MyViewController ()
@property (nonatomic) UIView *subView;
@property (nonatomic) CGFloat topValue;
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.subView = [[UIView alloc] init];
self.subView.backgroundColor = [UIColor blueColor];
[self.view addSubview:self.subView];
self.topValue = -128;
}
- (void)updateViewConstraints {
[self.subView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@(self.topValue));
make.width.equalTo(@128);
make.height.equalTo(@128);
make.leading.equalTo(@0);
}];
[super updateViewConstraints];
}
- (IBAction)show:(id)sender {
self.topValue = (self.topValue == 0) ? -128 : 0;
[self.view setNeedsUpdateConstraints];
[self.view updateConstraintsIfNeeded];
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
}];
}
我正在尝试使用 [UIView animationWithDuration]
方法通过约束为 UIView 设置动画。
我非常熟悉使用 UIView.frame 的动画,这非常简单。
在 init 方法中我添加了那些子视图
-(instancetype)init
{
if (self = [super init]) {
[self addSubview:self.bg_view];
[self.bg_view addSubview:self.content];
[self.bg_view addSubview:self.titleView];
[self.bg_view addSubview:self.reward];
}
return self;
}
和Masonry文件中显示的一样,约束设置在
-(void)updateConstraints
{
[self makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo([UIApplication sharedApplication].keyWindow);
}];
[self.bg_view makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
[self.titleView makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.bg_view.centerX);
make.top.equalTo(self.bg_view.top).with.offset(150);
}];
[self.content makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(self.bg_view).with.offset(-k_Margin*2);
make.bottom.equalTo(self.reward.top).with.offset(-20);
make.centerX.equalTo(self.bg_view);
make.top.equalTo(self.titleView.bottom).with.offset(20);
}];
[self.reward makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.bg_view.centerX);
make.top.equalTo(self.content.bottom).with.offset(20);
make.bottom.equalTo(self.bg_view.bottom).with.offset(-40);
}];
[super updateConstraints];
}
并且在show方法中
-(void)show
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
[self setNeedsUpdateConstraints];
[UIView animateWithDuration:5.0f animations:^{
/* I have tried variety ways to get the uiview come into the
screen from the top*/
}];
}
我在下面发布了一些代码来帮助您实现此目的,我使用了 viewcontroller,如果您愿意,您可以像 Masonry 示例中那样使用 UIView。我在我的视图上放置了一个按钮并将其连接到我的 'show' 方法。
看看 Masonry 项目中的 iOS 示例,它们很有帮助。
@interface MyViewController ()
@property (nonatomic) UIView *subView;
@property (nonatomic) CGFloat topValue;
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.subView = [[UIView alloc] init];
self.subView.backgroundColor = [UIColor blueColor];
[self.view addSubview:self.subView];
self.topValue = -128;
}
- (void)updateViewConstraints {
[self.subView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@(self.topValue));
make.width.equalTo(@128);
make.height.equalTo(@128);
make.leading.equalTo(@0);
}];
[super updateViewConstraints];
}
- (IBAction)show:(id)sender {
self.topValue = (self.topValue == 0) ? -128 : 0;
[self.view setNeedsUpdateConstraints];
[self.view updateConstraintsIfNeeded];
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
}];
}