简体   繁体   English

如何复制消息在iOS 7中弹跳气泡

[英]How to replicate Messages bouncing bubbles in iOS 7

When you are in a conversation within the Messages app in iOS 7, if you scroll up or down you will notice that the bubbles and more so the text saying when the messages were sent, will bounce into place. 当您在iOS 7中的消息应用程序中进行对话时,如果向上或向下滚动,您会注意到气泡以及发送消息时所说的文本将更多地反弹到位。

I am trying to replicate this in my own table view, but am not finding a way to do it. 我试图在我自己的表视图中复制它,但我没有找到一种方法来做到这一点。

I assume it is using UIDynamics, but I am not sure how to tie that in with the scrolling and the content bouncing. 我假设它使用的是UIDynamics,但我不确定如何将其与滚动和内容弹跳联系起来。

Any help would be appreciated 任何帮助,将不胜感激

If you want to replicate that effect yourself, you need UIKit Dynamics. 如果您想自己复制该效果,则需要UIKit Dynamics。 I was interested in that effect myself and it was explained at WWDC this year. 我自己对这种效果很感兴趣,并且今年在WWDC上进行了解释。

Take a look at WWDC Session 217: Exploring Scroll Views on iOS 7 看一下WWDC Session 217:探索iOS 7上的滚动视图

There are also read to use components on the internet, such as THSpringyCollectionView 还可以阅读互联网上的组件,例如THSpringyCollectionView

我对这个效果也感兴趣,在我的网络研究中,我发现了这个教程 - http://www.objc.io/issue-5/collection-views-and-uidynamics.html它正在实现同样的想法。

在此输入图像描述

You can add a springy bounce to content in your scrollview like this: 您可以在滚动视图中为内容添加弹性反弹,如下所示:

  1. Set up a UIScrollview and add to your view. 设置UIScrollview并添加到您的视图中。

     mainScroller = [UIScrollView new]; mainScroller.frame = CGRectMake(0, 0, w, h); mainScroller.bounces = true; mainScroller.pagingEnabled = false; mainScroller.delegate = self; [self.view addSubview:mainScroller]; 
  2. Layout a UIView and add it within your scrollview. 布局UIView并将其添加到您的滚动视图中。

     contentView = [UIView new]; contentView.frame = CGRectZero; [mainScroller addSubview:contentView]; 
  3. Add subviews your your 'contentView'. 添加您的'contentView'的子视图。

     UIView * unitView = [UIView new]; unitView.frame = CGRectMake(0, yOff, w, 280); [contentView addSubview:unitView]; 
  4. Resize both contentView and scrollview to fit the content. 调整contentView和scrollview的大小以适合内容。 Below w is view width and yOff the total cumulative height of the content. w下面是视图宽度, y是内容的总累积高度。

     contentView.frame = CGRectMake(0, 0, w, yOff); mainScroller.contentSize = CGSizeMake(w, yOff); 
  5. In your scrollViewDidScroll method, add the following code: 在scrollViewDidScroll方法中,添加以下代码:

      float y = mainScroller.contentOffset.y; contentView.transform = CGAffineTransformMakeTranslation(0, y); for (UIView * sub in contentView.subviews){ int index = (int)[contentView.subviews indexOfObject:sub]; float delay = index * 0.03; float duration = 1.0f - delay; [UIView animateWithDuration:duration delay:delay usingSpringWithDamping:0.8 initialSpringVelocity:0.7 options:UIViewAnimationOptionCurveEaseInOut| UIViewAnimationOptionAllowUserInteraction animations:^{ sub.transform = CGAffineTransformMakeTranslation(0, -y); } completion:^(BOOL finished){ }]; } 

The UIViewAnimationOptionAllowUserInteraction animation option allows for you to interact with the content immediately. UIViewAnimationOptionAllowUserInteraction动画选项允许您立即与内容交互。 Tweak the delay, duration, spring dampening and spring velocity variables to suit your needs. 调整延迟,持续时间,弹簧阻尼和弹簧速度变量以满足您的需求。

The code could be further adapted to allow for touch detection; 代码可以进一步适应以允许触摸检测; as it stands, the springy bounce originates at the top of the scrollView and descends down through the views, which for shorter scrollViews is barely noticeable. 如图所示,弹性反弹起源于scrollView的顶部,并通过视图下降,这对于较短的scrollViews几乎不可察觉。

EDIT: Touch detection 编辑:触摸检测

If you want to allow for touch detection, replace with these lines in your scrollViewDidScroll method: 如果要允许触摸检测,请在scrollViewDidScroll方法中替换为以下行:

int index = (int)[contentView.subviews indexOfObject:sub];
int deviation = abs(touchIndex - index);
float delay = deviation * 0.03;
float duration = 1.0f - delay;

Where the new variable touchIndex is defined like so: 新变量touchIndex的定义如下:

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    //grab the location of the touch event
    CGPoint location = [scrollView.panGestureRecognizer locationInView:scrollView];
    int yTouch = location.y; //grab y coordinate
    touchIndex = (yTouch - 100) / 100; //calculate the index of the cell, where 100 is the height of the cell

}

If you have dynamic cell heights, store them in an array eg 'selectedHeights'. 如果您具有动态单元格高度,请将它们存储在数组中,例如“selectedHeights”。 Then update your scrollViewWillBeginDragging method for the below, where the 'tally' float is set to 70 to allow for a header. 然后更新下面的scrollViewWillBeginDragging方法,其中'tally'浮点数设置为70以允许标题。

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    //grab the location of the touch event
    CGPoint location = [scrollView.panGestureRecognizer locationInView:scrollView];
    int yTouch = location.y; //grab y coordinate
    float tally = 70.0f;
    for (int n = 0; n < selectedHeights.count; n++){
        float cellHeight = [selectedHeights[n] floatValue];
        tally += cellHeight;
        if (tally > yTouch){
            touchIndex = n;
            break;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM