iOS使用約束實現動畫效果
我知道的方法有兩種:
1.在改變完約束後,在動畫塊內,使用方法layoutIfNeeded,可以實現一般普通的動畫效果。
#import "ViewController.h"
#import "Masonry.h"
#import "POP.h"
@interfaceViewController ()
/*!*/
@property (nonatomic,strong) UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[selflabel];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[supertouchesEnded:touches withEvent:event];
NSLayoutConstraint *leftConstraint = nil;
// POPSpringAnimation *positionAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayoutConstraintConstant];//kPOPLayerPositionY
POPBasicAnimation *positionAnimation = [POPBasicAnimationanimationWithPropertyNamed:kPOPLayoutConstraintConstant];
for (NSLayoutConstraint *constraint inself.view.constraints) {
if (constraint.firstItem == self.label && constraint.firstAttribute == NSLayoutAttributeLeft) {
leftConstraint = constraint;
break;
}
}
positionAnimation.duration = 1;
positionAnimation.fromValue = @(leftConstraint.constant);
positionAnimation.toValue = @(0);
// positionAnimation.springBounciness = 10;
[positionAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
[_labelmas_updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(100);
}];
}];
[leftConstraint pop_addAnimation:positionAnimation forKey:@"constantAnimation"];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Getter
- (UILabel *)label {
if (! _label) {
_label = [[UILabelalloc]init];
_label.backgroundColor = [UIColorcyanColor];
_label.text = @"what test123";
[self.viewaddSubview:_label];
[_labelmas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(300);
make.top.equalTo(self.view).offset(100);
}];
}
return_label;
}
@end