使用滑动显示/创建视图控制器

时间:2016-08-05 12:44:56

标签: ios objective-c iphone

我有UIViewController格式方法“使用索引创建”。

我想要构建的就像iPhone中的照片库,向左滑动 - 您创建索引为-1的视图控制器,向右滑动,您创建另一个索引为+1的控制器。

我使用了UIPageViewController,但由于某种原因,它在某些滑动后显示错误的页面,我无法弄清楚原因。

我的结果是什么,我只想使用 Swipe Gesture 来创建我的控制器。

但是,我需要动画,就像你向左或向右滚动一页一样,你可以看到动画如何呈现新的控制器,并在动画时间显示“旧”控制器。现在我使用:

-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer
{
    //Do what you want here
    NSLog(@"did swiped left");
    [CalendarViewController createNext:-1];
    [self presentViewController:[CalendarViewController createNext:-1] animated:YES completion:nil];

}

-(void)swiperight:(UISwipeGestureRecognizer*)gestureRecognizer
{
    //Do what you want here
    NSLog(@"did swiped right");
    [CalendarViewController createNext:[CalendarViewController createNext:1]];
    [self presentViewController:[CalendarViewController createNext:1] animated:YES completion:nil];

}

有没有办法实现动画,比如当您向左或向右滑动照片时?现在看起来像是模态呈现的新vc。

2 个答案:

答案 0 :(得分:1)

尝试使用以下代码进行左侧滑动使用转换子类型kCATransitionFromLeft您想要什么

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let vc = storyBoard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
        let transition: CATransition = CATransition()
        transition.duration = 0.5
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        transition.type = kCATransitionReveal
        transition.subtype = kCATransitionFromRight
        self.view.window!.layer.addAnimation(transition, forKey: nil)
        self.presentViewController(vc, animated:false, completion:nil)

答案 1 :(得分:1)

如果您想要添加效果,那么您必须添加两个滑动手势识别器,以检测左滑动和右滑动。

你应该配置自己的过渡效果,它会显示你的viewcontroller类推或弹出效果。您可以通过以下方式实现它,

用于展示View Controller(从右向左滑动),

CATransition *customTransition = [CATransition animation];
customTransition.duration = 0.3;
customTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
customTransition.type = kCATransitionPush;
customTransition.subtype = kCATransitionFromRight;
[self.view.window.layer addAnimation:customTransition forKey:nil];

[self presentViewController:yourViewControllerToBePresent animated:YES completion:nil];

对于解雇视图控制器(从左到右滑动我假设!),

  CATransition *customTransitionDismiss = [CATransition animation];
customTransitionDismiss.duration = 0.3;
customTransitionDismiss.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
customTransitionDismiss.type = kCATransitionPush;
customTransitionDismiss.subtype = kCATransitionFromLeft;
[self.view.window.layer addAnimation:customTransitionDismiss forKey:nil];
[self dismissViewControllerAnimated:YES completion:nil];

但我认为这不是最佳方法。

如果你有很多页面,那么你应该使用UIPageViewControllerUICollectionView使用paging scroll effect的单个单元格,这将deque单元格并且将节省内存!! < / p>