【发布时间】:2013-07-21 20:31:29
【问题描述】:
我希望能够同时从位置 A 淡出 UIView 并在位置 B 淡入 。这在 iOS 上可行吗?
【问题讨论】:
标签: ios animation uiview uikit fade
我希望能够同时从位置 A 淡出 UIView 并在位置 B 淡入 。这在 iOS 上可行吗?
【问题讨论】:
标签: ios animation uiview uikit fade
一种方法是创建视图的图像,将其放在屏幕上(在图像视图中)而不是实际视图,将视图的 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;
}
【讨论】:
是的,大致如下:
[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;
}];
}];
【讨论】: