我正在尝试使用自动版面设置视图,以便在视图更改大小时可以很好地设置动画效果...
情况是...
我有一个UIView
和一个名为animView
的子视图。 animView
在视图框架之外开始。
随着视图的增大(变高),animView
以相同的速率向下移动。
当animView
从视图顶部起10点时(即@“V:| -10- [animView]”),它停止并“粘在”该点。
即像这样的东西...
最佳答案
这有效(Dropbox share for the project):
诀窍是使用不可见的UIView作为间隔对象。您称为animView的视图就是这样设置的。因为我在IB中对此进行了部分设置,所以它仍然会引起一些问题,但是您会看到它可以按需运行。
@implementation AnimView {
UIView * _innerBox;
UIView * _spacer;
}
然后从
- setUpEverything
或initWithCoder:
调用方法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]];
}