iOS/Objective-C:transitionWithView自定义动画块立即调整视图大小,而不是持续时间
下面的代码是我在UIViewController中创建的一个方法,用于在控制器自己的视图上弹出/向下显示“reader”覆盖。其目的是让读者在一个特定的点上以透明的、大小为零的方式开始阅读。“弹出窗口”随后随着不透明度和大小的增加而设置动画,并向应用程序框架中心位置移动。“弹出”随后被设置为反向动画,在向指定位置移动时向后收缩,逐渐消失iOS/Objective-C:transitionWithView自定义动画块立即调整视图大小,而不是持续时间,objective-c,ios,uiview,size,uiviewanimationtransition,Objective C,Ios,Uiview,Size,Uiviewanimationtransition,下面的代码是我在UIViewController中创建的一个方法,用于在控制器自己的视图上弹出/向下显示“reader”覆盖。其目的是让读者在一个特定的点上以透明的、大小为零的方式开始阅读。“弹出窗口”随后随着不透明度和大小的增加而设置动画,并向应用程序框架中心位置移动。“弹出”随后被设置为反向动画,在向指定位置移动时向后收缩,逐渐消失 -(void)popupReader:(BOOL)isPopup from:(CGPoint)loc { CGFloat newAlpha = 0.0
-(void)popupReader:(BOOL)isPopup from:(CGPoint)loc {
CGFloat newAlpha = 0.0f;
CGPoint newCenter = CGPointZero;
CGRect newBounds = CGRectZero;
CGRect appFrame = [UIScreen mainScreen].applicationFrame;
CGSize readerSize = [self viewSize];
if (isPopup) {
newAlpha = 1.0f;
newCenter = CGPointMake(appFrame.origin.x+appFrame.size.width/2,
appFrame.origin.y+appFrame.size.height/2);
newBounds = CGRectMake(0,0,readerSize.width,readerSize.height);
[self.view setAlpha:0.0f];
[self.view setCenter:loc];
[self.view setBounds:CGRectMake(0, 0, 0, 0)];
} else {
newCenter = loc;
newBounds = CGRectMake(0,0,1,1);
}
const CGFloat animDur = 0.3f;
[UIView transitionWithView:self.view
duration:animDur
options:UIViewAnimationOptionTransitionNone|UIViewAnimationOptionCurveEaseOut
animations:^{
self.view.alpha = newAlpha;
self.view.center = newCenter;
self.view.bounds = newBounds;
}
completion:nil];
}
弹出代码完全按照需要工作。但是,弹出式版本(即,如果ispoup==NO,则执行代码)会立即更改边界,而不是逐渐更改。因此,弹出式动画从一开始就显示一个1像素的方形视图,该视图朝着其目标移动并逐渐消失
-(void)popupReader:(BOOL)isPopup from:(CGPoint)loc {
CGFloat newAlpha = 0.0f;
CGPoint newCenter = CGPointZero;
CGRect newBounds = CGRectZero;
CGRect appFrame = [UIScreen mainScreen].applicationFrame;
CGSize readerSize = [self viewSize];
if (isPopup) {
newAlpha = 1.0f;
newCenter = CGPointMake(appFrame.origin.x+appFrame.size.width/2,
appFrame.origin.y+appFrame.size.height/2);
newBounds = CGRectMake(0,0,readerSize.width,readerSize.height);
[self.view setAlpha:0.0f];
[self.view setCenter:loc];
[self.view setBounds:CGRectMake(0, 0, 0, 0)];
} else {
newCenter = loc;
newBounds = CGRectMake(0,0,1,1);
}
const CGFloat animDur = 0.3f;
[UIView transitionWithView:self.view
duration:animDur
options:UIViewAnimationOptionTransitionNone|UIViewAnimationOptionCurveEaseOut
animations:^{
self.view.alpha = newAlpha;
self.view.center = newCenter;
self.view.bounds = newBounds;
}
completion:nil];
}
我已经尝试过只设置帧的动画,而不是边界和中心,但是结果是一样的
有人知道为什么会发生这种情况,以及我如何克服这个问题吗
非常感谢您抽出时间。来自文档:
UIViewAnimationOptionTransitionNone-
用于指定不应发生转换的选项
尝试以下方法之一:
UIViewAnimationOptionTransitionFlipFromLeft
UIViewAnimationOptionTransitionFlipFromRight
UIViewAnimationOptionTransitionCurlUp
UIViewAnimationOptionTransitionCurlDown
编辑:
或者,如果您只是希望设置这些值的动画,请尝试
[UIView animateWithDuration:(NSTimeInterval)duration
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion];
感谢bshirley,animateWithDuration方法并没有很奇怪地解决这个问题,但是使用一个过渡确实解决了这个问题:)非常感谢!