------from iOS_Animations_by_Tutorials_v2
https://github.com/LomyCool/iOSAnimation/tree/master/aotulayout
1.在xib添加的约束,在代码中如何修改?
2.如何通过代码添加约束?
//遍历父控件的约束,通过 firstItem 和firstAttribute 来确定某一约束,或者通过 identifier(xib必须先设置好)确定,
for (NSLayoutConstraint * con in _titleLabel.superview.constraints) {
if (con.firstItem == _titleLabel && con.firstAttribute == NSLayoutAttributeCenterX) {
con.constant = isMenuOpen ? -100:0;
continue;
}
if ([con.identifier isEqualToString:@"TitleCenterY"]) {
con.active = false;
[_titleLabel.superview removeConstraint:con];
//代码添加约束
NSLayoutConstraint * newCon = [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_titleLabel.superview attribute:NSLayoutAttributeCenterY multiplier:isMenuOpen? 0.67 :1.0 constant:0.5];
newCon.identifier = @"TitleCenterY";
newCon.active = YES;
continue;
}
}
3.如何让约束产生动画效果? [self.view layoutIfNeeded];
// 旋转 + 按钮
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[self.view layoutIfNeeded];
CGFloat angle = isMenuOpen ? M_PI_4 : 0;
_menuBtn.transform = CGAffineTransformMakeRotation(angle);
} completion:^(BOOL finished) {
}];
4.如何给通过代码创建的控件添加约束?
//6.0
UIImageView * imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
imageView.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint * newCon = [NSLayoutConstraint constraintWithItem:_imageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_self.view attribute:NSLayoutAttributeCenterY multiplier:isMenuOpen? 0.67 :1.0 constant:0.5];
newCon.identifier = @"TitleCenterY";
newCon.active = YES;
//9.0 新出的方法
UIImageView * imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
imageView.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *conX = [imageView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor]; // centerX
NSLayoutConstraint * conBottom = [imageView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:imageView.frame.size.height]; // bottom 往下移动imageViewHeight距离
NSLayoutConstraint * conWidth = [imageView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier:0.33 constant:-50]; // 确定宽度
CGFloat scale = image.size.height / image.size.width;
NSLayoutConstraint * conHeight = [imageView.heightAnchor constraintEqualToAnchor:imageView.widthAnchor multiplier:scale]; // 宽高相等
[NSLayoutConstraint activateConstraints:@[conX,conBottom,conHeight,conWidth]];
[self.view layoutIfNeeded];
5.如何给刚创建出来的控件,添加动画效果?
重写 didMoveToSuperview,
-(void)didMoveToSuperview{
[super didMoveToSuperview];
if (!self.superview) {
return;
}
[UIView animateWithDuration:1 delay:0.03 usingSpringWithDamping:0.3 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.alpha = 1.0;
CGPoint center = self.center;
center.x -= self.superview.bounds.size.width;
self.center = center;
} completion:^(BOOL finished) {
}];
}