IOS7中ViewController切换动画


IOS的API给出了两种切换ViewController方式:

1、

- (void)presentViewController: animated: completion:

- (void)dismissViewControllerAnimated: completion:

2、(这里不做展开)

- (void)pushViewController: animated:
- (UIViewController *)popViewControllerAnimated:

IOS7可以自定义这两种方式的切换动画,要怎样实现呢?首先就要说下@Protocol UIViewControllerAnimatedTransitioning这个协议。

UIViewControllerAnimatedTransitioning协议需实现以下方法:

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext  在这里根据上下文(UIViewControllerContextTransitioning)返回切换动画时间。

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext  在这里根据上下文自定义切换动画。


上面提到的上下文UIViewControllerContextTransitioning,其为开发者提供了切换相关的内容,包含从哪儿ViewController切换到哪个ViewController的相关信息。这个API的重要方法包括:

- (UIViewController *)viewControllerForKey:(NSString *)key  通过提供一个key来获取切出或切入的Viewcontroller,SDK中key包括有UITransitionContextFromViewController和UITransitionContextToViewController 分别表示切出切入。

- (UIView *)containerView  切换发生时的容器,开发者应该- (void)animateTransition:方法中将切出ViewController总容器中移除,将切入ViewController添加到容器中。

- (CGRect)initialFrameForViewController:(UIViewController *)vc  获取某个ViewController的初始frame

- (CGRect)finalFrameForViewController:(UIViewController *)vc  获取某个ViewController切换结束时的frame

- (void)completeTransition:(BOOL)didComplete  报告切换已经完成


以上介绍完UIViewControllerAnimatedTransitioning协议的一些方法以及相关的一些信息,下面将讲UIViewControllerAnimatedTransitioning该怎样用。

1、首先是在开头处提到的第一种切换方式中使用UIViewControllerAnimatedTransitioning来自定义切换动画。

要讲这个,得先了解 UIViewControllerTransitioningDelegate,通过实现这个协议的两个方法(切入ViewController的transitioningDelegate需设置为实现改协议的对象):

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    return self.drawerAnimation;
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    return self.popAnimation;
}
两个方法返回UIViewControllerAnimatedTransitioning对象实现自定义切换动画。


2、第二种切换方式使用UIViewControllerAnimatedTransitioning实现自定义切换动画。

这种切换方式是通过UINavigationController来实现的,可自定义实现UINavigationControllerDelegate的UINavigationController来自定义切换动画,自定义的UINavigationController实现一下方法:

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
    if (operation == UINavigationControllerOperationPush) {
        return self.drawerAnimation;
    }
    return self.animation;
}
通过operation分辨push还是pop来分别实现切入切出动画,同样,这个方法返回UIViewControllerAnimatedTransitioning对象。


以上是对IOS7的ViewController切换动画自定义的一些简单说明,博主文字功底不好,表述有不对请指正。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS ,要实现点击单元格切换视图的功能,有以下几个步骤: 1. 创建一个 UITableView,并设置其代理和数据源。 2. 在代理方法 `tableView(_:didSelectRowAt:)` ,监听单元格的点击事件。 3. 在点击事件,获取点击的单元格索引,然后根据需要进行视图切换。 下面是一个简单的示例代码: ```swift import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let tableView = UITableView() override func viewDidLoad() { super.viewDidLoad() // 设置tableView的frame和代理 tableView.frame = view.bounds tableView.delegate = self tableView.dataSource = self // 注册单元格 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") // 添加tableView到视图 view.addSubview(tableView) } // 返回单元格数量 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } // 创建和配置单元格 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = "Cell \(indexPath.row + 1)" return cell } // 单元格点击事件 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // 获取点击的单元格索引 let selectedRow = indexPath.row // 根据需要进行视图切换 switch selectedRow { case 0: // 切换到视图1 let viewController1 = ViewController1() navigationController?.pushViewController(viewController1, animated: true) case 1: // 切换到视图2 let viewController2 = ViewController2() navigationController?.pushViewController(viewController2, animated: true) // 其他切换逻辑... default: break } // 取消选效果 tableView.deselectRow(at: indexPath, animated: true) } } ``` 在上述示例,我们创建了一个包含 5 个单元格的 UITableView,并通过点击单元格来切换到不同的视图。你可以根据自己的需求,定义不同的视图,并在 `didSelectRowAt` 方法进行切换逻辑的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值