自己动手编写一些简单的转场动画

3 篇文章 0 订阅
1 篇文章 0 订阅

有时候在项目开发的时候可能会有制作一些特殊的转场动画的需求,比如实现像开门一样的动画,视图从中间裂开,然后分别从两边飞出。
首先有个很实用的函数来截取当前屏幕的图片,这里的参数第一个一般填写self.view,第二个参数可以根据实际需要来写frame

-(UIImage*)captureView:(UIView *)theView frame:(CGRect)fra{
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theView.layer renderInContext:context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRef ref = CGImageCreateWithImageInRect(img.CGImage, fra);
    UIImage *i = [UIImage imageWithCGImage:ref];
    CGImageRelease(ref);
    return i;
}

这样就获取了整个屏幕的截屏图片,然后就可以设置动画可以实现左右开门或者上下打开飞出去,
-(void)pushViewController:(UIViewController *)nextViewController{
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;

UIImage *redayShowVC = [self imageWithView:nextViewController.view];
UIImageView *redayShowView = [[UIImageView alloc] init];
redayShowView.image = redayShowVC;
redayShowView.hidden = YES;

// redayShowView.frame = CGRectMake(0 , 0, kScreenWidth, kScreenHeight);
redayShowView.frame = CGRectMake(self.view.center.x , self.view.center.y, 1, 1);
[self.view addSubview:redayShowView];

//先截取当前屏幕的图片
UIImage *currenScreenImage = [self captureView:appDelegate.window frame:CGRectMake(0, 0, kScreenWidth, kScreenHeight / 2)];
UIImageView *topHalf = [[UIImageView alloc] init];
topHalf.image = currenScreenImage;
topHalf.clipsToBounds = YES;
//截取屏幕下半部分的图片
UIImage *currenScreenImageBottom = [self captureView:appDelegate.window frame:CGRectMake(0, kScreenHeight / 2, kScreenWidth, kScreenHeight / 2)];
UIImageView *bottomHalf = [[UIImageView alloc] init];
bottomHalf.image = currenScreenImageBottom;
bottomHalf.clipsToBounds = YES;

[self.view addSubview:topHalf];
[self.view  addSubview:bottomHalf];


topHalf.frame = CGRectMake(0, 0,kScreenWidth, kScreenHeight / 2);
bottomHalf.frame = CGRectMake(0,kScreenHeight / 2, kScreenWidth, kScreenHeight / 2);
//实现动画
[UIView animateWithDuration:1.0 animations:^{
    topHalf.frame = CGRectMake(0, - kScreenHeight / 2, kScreenWidth, kScreenHeight / 2);
    bottomHalf.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight / 2);
    redayShowView.hidden = NO;
    //这句是缩放函数 可以注释掉
    redayShowView.transform = CGAffineTransformMakeScale(kScreenWidth, kScreenHeight);

} completion:^(BOOL finished) {
    [redayShowView removeFromSuperview];
    [topHalf removeFromSuperview];
    [bottomHalf removeFromSuperview];
    [self.navigationController pushViewController:nextViewController animated:NO];
}];

}

这样一个效果就出来了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值