UIView翻转animation
在我的游戏中,我有一个更小的UIViews的网格托pipe在我的主UIView。 随机的框会变成一个不同的颜色,在这一点上,用户可以触摸他们得分。 当他们触摸盒子时,我想展示某种animation,理想情况下类似于XCode原生提供的模式水平翻转细节。 我怎样才能做这个animation,而不需要实际转换到另一个UIView?
你可以简单地尝试和animation这样的视图变换(垂直翻转):
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void) { view.transform = CGAffineTransformMakeScale(1, -1); } completion:nil];
或者为了更好的控制,你可以看看iOS-Flip-Transform 。
编辑:
对于影子来说,试试这个:
view.layer.shadowColor = [UIColor blackColor].CGColor; view.layer.shadowOpacity = 0.75; view.layer.shadowRadius = 15.0; view.layer.shadowOffset = (CGSize){0.0,20.0}; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void) { view.transform = CGAffineTransformMakeScale(1, -1); } completion:^(BOOL b) { view.layer.shadowColor = [UIColor clearColor].CGColor; view.layer.shadowOpacity = 0.0; view.layer.shadowRadius = 0.0; view.layer.shadowOffset = (CGSize){0.0, 0.0}; }];
我希望这适合你。 您可以随意更改阴影设置。 不要忘记导入QuartzCore / QuartzCore.h。
UIView提供了一个叫做的方法
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
选项包括UIViewAnimationOptionTransitionFlipFromLeft
和… Right
对于每个应该翻转的框,你可以使用一个容器UIView。 将两边添加到此容器中。
if(side1Visible) UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationTransitionFlipFromRight; [UIView transitionWithView:containerView duration:1.0 options:options animations:^{ side1.hidden = YES; side2.hidden = NO; } completion:NULL]; else{ UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationTransitionFlipFromLeft; [UIView transitionWithView:containerView duration:1.0 options:options animations:^{ side1.hidden = NO; side2.hidden = YES; } completion:NULL]; }
你可以用Swift 3.0的下面的代码来做到这一点:
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseInOut, animations: { self.view.transform = CGAffineTransform(scaleX: 1, y: -1) }, completion: nil)
请记住,这个动作将发生在你的主线程中,所以在执行过程中应用程序将被阻塞。
如果你需要一个完成处理程序,有几个选项
这就是我以Ilker Baltaci的回答结束的(在Swift 3中 ):
@IBAction func flipViews() { // declare variables for transition let duration = 0.5 var animationOptions: UIViewAnimationOptions var animations: () -> Void var completion: ((Bool) -> Void)? if view2.isHidden { animationOptions = [.beginFromCurrentState, .transitionFlipFromTop] animations = { self.view2.isHidden = false self.view1.isHidden = true } completion = { success in if success { self.updateSomeContentOfView2() } } } else { animationOptions = [.beginFromCurrentState, .transitionFlipFromBottom] animations = { self.view2.isHidden = true self.view1.isHidden = false } } UIView.transition(with: containerView, duration: duration, options: animationOptions, animations: animations, completion: completion) }
我的视图层次为containerView
, view1
& view2
(这是我的自定义ViewController类中的所有IBOutlets
)如下所示:
-
containerView
-
view1
-
view2
-