12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- //
- // MASExampleUpdateView.m
- // Masonry iOS Examples
- //
- // Created by Jonas Budelmann on 3/11/13.
- // Copyright (c) 2013 Jonas Budelmann. All rights reserved.
- //
- #import "MASExampleUpdateView.h"
- @interface MASExampleUpdateView ()
- @property (nonatomic, strong) UIButton *growingButton;
- @property (nonatomic, assign) CGSize buttonSize;
- @end
- @implementation MASExampleUpdateView
- - (id)init {
- self = [super init];
- if (!self) return nil;
- self.growingButton = [UIButton buttonWithType:UIButtonTypeSystem];
- [self.growingButton setTitle:@"Grow Me!" forState:UIControlStateNormal];
- self.growingButton.layer.borderColor = UIColor.greenColor.CGColor;
- self.growingButton.layer.borderWidth = 3;
- [self.growingButton addTarget:self action:@selector(didTapGrowButton:) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:self.growingButton];
- self.buttonSize = CGSizeMake(100, 100);
- // make sure updateConstraints gets called
- [self setNeedsUpdateConstraints];
- return self;
- }
- // this is Apple's recommended place for adding/updating constraints
- - (void)updateConstraints {
- [super 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);
- make.height.lessThanOrEqualTo(self);
- }];
- }
- - (void)didTapGrowButton:(UIButton *)button {
- self.buttonSize = CGSizeMake(self.buttonSize.width * 1.3, self.buttonSize.height * 1.3);
- // 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];
- }];
- }
- @end
|