![](/img/trans.png)
[英]presentModalViewController doesn't rotate properly in iPhone / iPad
[英]iPhone/iPad - Core Animation - CGAffineTransformMakeTranslation doesn't modify the frame
我面临一个非常烦人的问题。 这是上下文:我有一个“矩形”视图,它是主视图的子视图。 我想要做的很简单,当我点击一个按钮时,我希望“矩形”视图在x轴上平移以便消失。 然后我添加一个新的子视图并进行翻译,以取代之前的“矩形”视图。 它的工作正常,但是如果我再次按下按钮,动画将从屏幕开始,就像CGAffineTransformMakeTranslation没有改变我的新“矩形”视图的框架一样。 这是代码:
UIView *rectangleView = [detailView viewWithTag:4]; //the actual frame is (20.0, 30.0, 884.0, 600.0)
[UIView animateWithDuration:0.5 animations:^{
[rectangleView setTransform:CGAffineTransformMakeTranslation(-1000, 0)];
} completion:^(BOOL finished) {
[rectangleView removeFromSuperview];
UIView *otherView = [[UIView alloc] initWithFrame:CGRectMake(1020.0, 30.0, 884.0, 600.0)];
[otherView setBackgroundColor:[UIColor purpleColor]];
[otherView setTag:4];
[detailView addSubview:otherView];
[UIView animateWithDuration:0.5 animations:^{
[otherView setTransform:CGAffineTransformMakeTranslation(-1000, 0)];
} completion:^(BOOL finished) {
[otherView release];
}];
}];
添加第二个视图后,您已将其变换设置为等于CGAffineTransformMakeTranslation(-1000, 0)
,并且当您想要删除该视图时,您将设置完全相同的变换 - 因此它将不起作用。 这里有2个选项:
将转换应用于视图已具有的转换:
CGAffineTransform newTransform = CGAffineTransformConcat(rectangleView.transform, CGAffineTransformMakeTranslation(-1000, 0)); [rectangleView setTransform:newTransform];
而不是应用变换直接操作视图位置(例如通过其中心属性)
UIView *rectangleView = [detailView viewWithTag:4]; //the actual frame is (20.0, 30.0, 884.0, 600.0) CGAffineTransform tf = CGAffineTransformMakeTranslation(-1000, 0); [UIView animateWithDuration:0.5 animations:^{ [rectangleView setCenter: CGPointApplyAffineTransform(rectangleView.center, tf)]; } completion:^(BOOL finished) { [rectangleView removeFromSuperview]; UIView *otherView = [[UIView alloc] initWithFrame:CGRectMake(1020.0, 30.0, 884.0, 600.0)]; [otherView setBackgroundColor:[UIColor purpleColor]]; [otherView setTag:4]; [detailView addSubview:otherView]; [UIView animateWithDuration:0.5 animations:^{ [otherView setCenter: CGPointApplyAffineTransform(otherView.center, tf)]; } completion:^(BOOL finished) { [otherView release]; }]; }];
尝试动画center
属性而不是使用仿射变换。 转换不是附加的,因此您的第二个动画(当您新添加的细节视图随后移出屏幕时)实际上根本不会改变视图,因为它已经应用了该转换(-1000,0)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.