代码片段之Masonry 按钮放大缩小动画简单实践

// UIView如果使用AutoLayout 必须写此方法

+ (BOOL)requiresConstraintBasedLayout
{
    return YES;
}

//按钮的动态效果修改时 此方法为 系统自带方法

// this is Apple's recommended place for adding/updating constraints
- (void)updateConstraints {

    [self.growingButton updateConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(@(self.buttonSize.width)).priorityLow();
        make.height.equalTo(@(self.buttonSize.height)).priorityLow();
        make.width.lessThanOrEqualTo(self); //按钮的最大宽度 <= 当前self的宽度
        make.height.lessThanOrEqualTo(self); //按钮的最大高度 <= 当前self的宽度
    }];
    
    //according to apple super should be called at end of method
    [super updateConstraints]; //苹果规定最后调用这个
}

//按钮触发的时候

1.   [self setNeedsUpdateConstraints]; //必须调用
2.   [self updateConstraintsIfNeeded]; //更新约束
3.   [UIView animateWithDuration:0.4 animations:^{
        [self layoutIfNeeded];
      }];   //动画效果

//代码演示

- (void)didTapGrowButton:(UIButton *)button {
    if (!self.zoom) {
        self.buttonSize = CGSizeMake(self.buttonSize.width * 5, self.buttonSize.height * 6);
        self.zoom = YES;
    } else {
        self.buttonSize = CGSizeMake(self.buttonSize.width / 5, self.buttonSize.height /  6);
        self.zoom = NO;
    }

    // tell constraints they need updating
    [self setNeedsUpdateConstraints];

    // update constraints now so we can animate the change
    [self updateConstraintsIfNeeded];

    [UIView animateWithDuration:0.4 animations:^{
        [self layoutIfNeeded];
    }];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 备胎 备胎? 在我的爱情字典里,没有备胎这个词。或者其实我也没有一本爱情字典。我对男女间感情的定义是,遇见了感觉心...
    池俊秀阅读 3,783评论 0 49
  • 站在2017这个时间点上,回望研究生生涯论文制作的时候,经历了问卷搜寻数据,用spss分析处理数据,做成论文输出的...
    yutingle116阅读 4,221评论 4 5
  • 在需要使用地理位置的时候就需要打开使用地理位置的权限.很多时候用户在第一时间就直接关闭了使用地理位置的权限,那么用...
    宗驭阅读 5,324评论 1 4
  • 控制情绪应该是人性里最重要也最难的事情。所谓控制住,并不是喜怒不形于色那么回事。那只能说城府深埋心底。但内心深...
    心如蔚蓝阅读 2,507评论 0 0
  • 这一周主要是教师端的招生bug修复问题。 下一周任务 继续修复招生bug;推古诗打卡模块。
    Kevinxuelei阅读 1,499评论 0 0

友情链接更多精彩内容