【问题标题】:Ways to switch views by swipe通过滑动切换视图的方法
【发布时间】:2011-07-03 09:09:35
【问题描述】:

我正在构建显示新闻文章的 iPhone 应用程序。

为了阅读一篇文章,我有一个包含 UITableView 的视图,用于显示文章的内容。

我需要什么: 我希望用户能够用手指滑动到下一篇文章,效果就像苹果的pageControl示例,或者像在图片之间滑动一样。(来回移动视觉效果)

问题: 唯一的方法是使用 UIScrollView 和“页面”, 还是有简单的方法,包括检测滑动+操纵开关效果+推送视图控制器?

我也想过的另一个方向是,如果我可以用一个水平 tableview 创建一个视图,每个单元格将显示一篇文章,用户将滑动移动到下一个单元格。

顺便说一句-iphone的fox新闻应用有这个功能,但他们似乎使用“页面控制”解决方案。

任何信息都会有所帮助。

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

    页面控制不是我认为的唯一解决方案。 想象一下,您必须在 viewController 中查看视图,然后当您检测到滑动及其方向时, 你所要做的就是:

    1. 将内容添加到将出现的视图中
    2. 将视图添加为视图控制器的子视图
    3. 将该视图放置在正确的一侧(如果用户向右滑动,则放置在左侧)
    4. 最终动画视图移动(位置)到正确的方向

    你已经完成了......

    这是一个例子:

    // in your uiviewcontroller subclass
    UIVIew *viewOne, *viewTwo;
    
    -(void)swipeFromView:(UIVIew *)visibleView
                  toView:(UIView *)pushingView
               direction:(UISwipeGestureRecognizerDirection)aDirection {
        // assuming the content of pushingView already set
        CGPoint visibleViewCenter = [visibleView center]; // register the center
        CGRect visibleViewFrame = [visibleView frame];
        CGPoint pushingViewCenter, visibleViewNewCenter;
    
        visibleViewNewCenter.y = visibleViewCenter.y;
        pushingViewCenter.y = visibleViewCenter.y;
    
        // I use 2 here, but you would make more calculations
        pushingViewCenter.x = 2 * visibleViewFrame.width;
        visibleViewNewCenter.x = -1 * 2 * visibleViewFrame.width;
        // reversing x if direction is right
        if(aDirection == UISwipeGestureRecognizerDirectionRight) {
            pushingViewCenter.x *= -1;
            visibleViewNewCenter.x *= -1;
        }
        // once the tmp center for pushingView is calculated
        // set it, and run the animation
        [pushingView setCenter:pushingViewCenter];
        [UIVIew animateWithDuration:theDurationYouWant
                         animations:^{
            [visibleView setCenter:visibleViewNewCenter];
            [pushingView setCenter:visibleViewCenter];
        }];
    }
    
    // somewhere else, you handle the swipe
    -(void)handleSwipeGesture:(UISwipeGestureRecognizer *)sender {
        // set content, etc
        [self swipeFromView:viewOne toView:viewTwo direction:[sender direction]];
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      相关资源
      最近更新 更多