我希望能够从位置 A 淡出 UIView 并同时在位置 B淡入。这在 iOS 上可行吗?
问问题
281 次
2 回答
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;
}];
}];
于 2013-07-21T20:38:20.557 回答
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;
}
于 2013-07-21T20:51:55.267 回答