是否可以使用淡入淡出将ui元素从一个位置移动到另一个位置?

时间:2013-07-21 20:31:29

标签: ios animation uiview uikit fade

我希望能够从位置A淡出UIView,同时在位置B 淡出它。这可能是iOS吗?

2 个答案:

答案 0 :(得分:0)

是这样的:

    [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;
    }];
 }];

答案 1 :(得分:0)

一种方法是创建视图图像,将其放在屏幕上(在图像视图中)在实际视图上,将视图的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;
}