【问题标题】:Is it possible to move a ui element from one location to the other using fading?是否可以使用淡入淡出将 ui 元素从一个位置移动到另一个位置?
【发布时间】:2013-07-21 20:31:29
【问题描述】:

我希望能够同时从位置 A 淡出 UIView 并在位置 B 淡入 。这在 iOS 上可行吗?

【问题讨论】:

    标签: ios animation uiview uikit fade


    【解决方案1】:

    一种方法是创建视图的图像,将其放在屏幕上(在图像视图中)而不是实际视图,将视图的 alpha 设置为 0,设置其框架(或调整布局约束)以将其放入一个新的位置,然后启动一个淡出图像的动画,并在实际视图中淡出。

    这样的事情应该可以工作:

    #import "ViewController.h"
    #import <QuartzCore/QuartzCore.h>
    
    @interface ViewController ()
    @property (weak,nonatomic) IBOutlet UIView *fadingView;
    @property (weak,nonatomic) IBOutlet NSLayoutConstraint *topCon;
    @property (strong,nonatomic) UIImageView *iv;
    @end
    
    @implementation ViewController
    
    -(IBAction)moveView:(id)sender {
        UIImage *viewimage = [self imageWithView:self.fadingView];
        self.iv = [[UIImageView alloc] initWithFrame:self.fadingView.frame];
        self.iv.image = viewimage;
        [self.view addSubview:self.iv];
        self.fadingView.alpha = 0;
        self.topCon.constant = 200; // topCon is IBOutlet to the top constraint to the superview
    
        [UIView animateWithDuration:1 animations:^{
            self.fadingView.alpha = 1;
            self.iv.alpha = 0;
        } completion:^(BOOL finished) {
            [self.iv removeFromSuperview];
        }];
    }
    
    - (UIImage *)imageWithView:(UIView *)view {
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.bounds.size.width, view.bounds.size.height), view.opaque, [[UIScreen mainScreen] scale]);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    }
    

    【讨论】:

      【解决方案2】:

      是的,大致如下:

          [UIView animateWithDuration:0.25 animations:^{
      
          self.viewA.frame = CGRectMake... // middle point
          self.viewA.alpha = 0.0f;
      
          }completion:^ (BOOL finished) 
       {
          [UIView animateWithDuration:0.25 animations:^{
      
          self.viewA.frame = CGRectMake... // final point
          self.viewA.alpha = 1.0f;
          }];
       }];
      

      【讨论】:

      • 但这最终只是淡出它。我需要另一个动画来淡入它。
      • 你可以链接动画,所以你可以淡出直到 X 位置,然后淡入直到最终目标。
      • 效果不错,但不是我想要的。本质上,我希望视图在它们消失时“同时出现在两个地方”。
      猜你喜欢
      • 2013-06-28
      • 2017-10-19
      • 1970-01-01
      • 2017-11-04
      • 2011-07-15
      • 1970-01-01
      • 2020-04-08
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多