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

一 概述

  • 通过参数传值设置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<UITouch *> *)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 效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值