我正在尝试使用自动布局设置视图,以便在视图更改大小时可以很好地动画...
情节是...
我有一个UIView
名为animView
. animView
在视图框架之外开始。随着视图的增长(变得更高),animView
以相同的速度向下移动。当animView
距离视图顶部 10 个点时(即 @"V:|-10-[animView]" ),它会停止并“粘”到该点。
即像这样的东西......
我正在尝试使用自动布局设置视图,以便在视图更改大小时可以很好地动画...
情节是...
我有一个UIView
名为animView
. animView
在视图框架之外开始。随着视图的增长(变得更高),animView
以相同的速度向下移动。当animView
距离视图顶部 10 个点时(即 @"V:|-10-[animView]" ),它会停止并“粘”到该点。
即像这样的东西......
这有效(项目的 Dropbox 共享):
诀窍是使用不可见的 UIView 作为间隔对象。您称为 animView 的视图是这样设置的。因为我在 IB 中部分设置了它,它仍然会引发一些问题,但你会看到它按你想要的方式工作。
@implementation AnimView {
UIView * _innerBox;
UIView * _spacer;
}
- setUpEverything
然后从initWithCoder:
or调用方法initWithFrame:
。
- (void) setUpEverything {
// Because I set this up in IB with AutoLayout off, I think it
// still needs this. Won't work without it
self.translatesAutoresizingMaskIntoConstraints = YES;
// Otherwise you will see the box outside of animView
self.clipsToBounds = YES;
_innerBox = [[UIView alloc] init];
_innerBox.backgroundColor = [UIColor blackColor];
_spacer = [[UIView alloc] init];
_spacer.backgroundColor = [UIColor clearColor];
_innerBox.translatesAutoresizingMaskIntoConstraints = NO;
_spacer.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_innerBox];
[self addSubview:_spacer];
// Give _innerBox and _spacer some dimensions to prevent ambiguity
NSLayoutConstraint *i02 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0f constant:50.0f];
NSLayoutConstraint *i03 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0f constant:50.0f];
NSLayoutConstraint *s02 = [NSLayoutConstraint constraintWithItem:_spacer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0f constant:50.0f];
// Center both views
NSLayoutConstraint *i05 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
NSLayoutConstraint *s04 = [NSLayoutConstraint constraintWithItem:_spacer attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
// Set the top of _innerBox 10.0 points from the top of the superview with a low priority
NSLayoutConstraint *i01 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0f constant:10.0f];
i01.priority = 250;
// Pin the spacer to the bottom of _innerBox;
NSLayoutConstraint *i04 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_spacer attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
// Pin the spacer's bottom to the bottom of the superview
NSLayoutConstraint *s01 = [NSLayoutConstraint constraintWithItem:_spacer attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
// Stretch the spacer out as needed
NSLayoutConstraint *s03 = [NSLayoutConstraint constraintWithItem:_spacer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:0 multiplier:1.0f constant:80.0f];
[self addConstraints:@[i01,i02,i03,i04,i05,s01,s02,s03,s04]];
}