IOS开发之——屏幕适配-AutoLayout动画(05)

IOS开发之——屏幕适配-AutoLayout动画(05),第1张

一 概述 通过参数传值设置AutoLayout的边距AutoLayout动画示例 二 通过参数传值设置AutoLayout的边距 2.1 修改前
NSArray *blueViewConsH= [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[blueView]-20-|" options:0 metrics:nil views:@{@"blueView":blueView}];
[self.view addConstraints:blueViewConsH];
2.2 设置margin参数
int margin=20;
//水平方向-设置蓝色View距离左边和右边20间距,设置X和宽度
NSArray *blueViewConsH= [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-margin-[blueView]-margin-|" options:0 metrics:@{@"margin":@(margin)} views:@{@"blueView":blueView}];
[self.view addConstraints:blueViewConsH];

说明:

constraintsWithVisualFormat中设置使用margin参数metrics中为margin赋值显示效果相同 三 AutoLayout动画示例 3.1 布局文件

Autolayout约束说明:

设置宽高都为80设置距离左边距0,距离上边距50代码中获取左边距leftCons,上边就topCons 3.2 代码
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topCons;

@end

@implementation ViewController

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //1.修改约束值
    self.leftCons.constant+=100;
    self.topCons.constant+=100;
    
    //2.让View上的约束执行动画
    [UIView animateWithDuration:5 animations:^{
        [self.view layoutIfNeeded];
    }];
}
@end
3.3 效果图

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/993623.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存