【发布时间】:2014-04-02 18:44:08
【问题描述】:
我正在尝试重现像 iOS 7 应用切换器这样的效果,我可以在其中滚动更小的、已转换的视图。我目前的障碍是获取 contentOffset
为了做到这一点,我在视图中添加了一个滚动视图,并将其调整到视图的边界。然后,我创建一个包含所有“卡片”的容器视图——我应用转换的正是这个容器视图。
无论如何,我遇到的问题是调整 where 特定卡片在应用了转换后出现 - 现在,我无法在转换期间以如下方式调整 contentOffset是光滑的。
无论如何,代码会有所帮助!这里是:
NSInteger idx = selectedCardIndex;
if (TransformApplied)
{
[UIView animateWithDuration:0.5 animations:^{
[self.container setTransform:CGAffineTransformIdentity];
[self.container setFrame:CGRectOffset(self.container.frame, (1-ScaleFactor)/2 * NumCards * ScreenWidth, 0)];
[self.scrollView setContentSize:self.container.frame.size];
[self.scrollView setContentOffset:CGPointMake(ScreenWidth * idx, 0) animated:NO];
[self.scrollView setPagingEnabled:YES];
} completion:^(BOOL finished) {
TransformApplied = NO;
}];
}
else
{
[UIView animateWithDuration:0.5 animations:^{
[self.container setTransform:CGAffineTransformMakeScale(ScaleFactor, ScaleFactor)];
[self.container setFrame:CGRectOffset(self.container.frame, (-1)*((1-ScaleFactor)/2) * NumCards * ScreenWidth, 0)];
[self.scrollView setContentSize:CGSizeMake(ScreenWidth * ScaleFactor * NumCards, ScreenHeight * ScaleFactor)];
[self.scrollView setContentOffset:CGPointMake(ScaleFactor * ScreenWidth * idx, 0) animated:NO];
[self.scrollView setPagingEnabled:NO];
} completion:^(BOOL finished) {
TransformApplied = YES;
}];
}
更多 我注意到在应用恒等变换时一切都很顺利,但在应用较小的变换时,我在动画之前的帧中得到了一个奇怪的“跳跃”。
如果我注释掉缩放变换(底部块)中的内容偏移调整,那么一切运行顺利,但是缩放变换后的 contentOffset 是错误的。
最后,调用 setContentOffset:animated: 会产生不稳定的动画,所以这是不行的。
非常感谢任何帮助!!!!
更新
我查看了 CALayer 上的锚点/位置属性,我的动画效果与此完美配合:
NSInteger idx = selectedCardIndex;
if (TransformApplied)
{
[UIView dd_AnimateWithDuration:0.5 animations:^{
[self.container setTransform:CGAffineTransformIdentity];
[self.scrollView setContentOffset:CGPointMake(ScreenWidth * idx, 0) animated:NO];
[self.scrollView setPagingEnabled:YES];
} completion:^(BOOL finished) {
TransformApplied = NO;
}];
}
else
{
CGPoint anchor = CGPointMake((idx * ScreenWidth + ScreenWidth/2)/CGRectGetWidth(self.container.frame), 0.5f);
[self.container.layer setAnchorPoint:anchor];
CGPoint position = CGPointMake((anchor.x) * self.container.frame.size.width, self.container.layer.position.y);
[self.container.layer setPosition:position];
[UIView dd_AnimateWithDuration:0.5 animations:^{
[self.container setTransform:CGAffineTransformMakeScale(ScaleFactor, ScaleFactor)];
[self.scrollView setPagingEnabled:NO];
} completion:^(BOOL finished) {
TransformApplied = YES;
}];
}
但是,现在我必须想办法在不改变容器位置的情况下消除滚动视图中的所有额外空间......
【问题讨论】:
标签: ios objective-c uiscrollview core-animation