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

当您在iOS 7中的消息应用程序中进行对话时,如果向上或向下滚动,则会注意到消息发出时的气泡以及更多的文字会弹起。

我试图在我自己的表格视图中复制这个,但是没有find办法去做。

我认为它是使用UIDynamics,但我不知道如何将绑定与滚动和内容弹跳。

任何帮助,将不胜感激

如果你想自己复制这个效果,你需要UIKit Dynamics。 我自己对这个效果感兴趣,今年在WWDC上解释了这个问题。

查看WWDC会话217:探索iOS 7上的滚动视图

也有阅读使用互联网上的组件,如THSpringyCollectionView

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

在这里输入图像说明

你可以像这样在你的滚动视图中添加一个弹性的反弹:

  1. 设置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. 布局UIView并将其添加到您的滚动视图中。

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

     UIView * unitView = [UIView new]; unitView.frame = CGRectMake(0, yOff, w, 280); [contentView addSubview:unitView]; 
  4. 调整contentView和scrollview以适应内容。 下面的w是视图宽度, y是内容的总累积高度。

     contentView.frame = CGRectMake(0, 0, w, yOff); mainScroller.contentSize = CGSizeMake(w, yOff); 
  5. 在您的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){ }]; } 

UIViewAnimationOptionAllowUserInteractionanimation选项允许您立即与内容进行交互。 调整延迟,持续时间,弹簧阻尼和弹簧速度variables以适应您的需求。

该代码可以进一步适用于允许触摸检测; 就目前而言,弹性反弹起源于scrollView的顶部,并通过视图向下,这对于较短的scrollViews几乎不可见。

编辑:触摸检测

如果您想允许触摸检测,请使用scrollViewDidScroll方法中的这些行进行replace:

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

新的variablestouchIndex定义如下:

 -(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 } 

如果你有dynamic的单元格高度,将它们存储在一个数组中,例如“selectedHeights”。 然后更新下面的scrollViewWillBeginDragging方法,其中'tally'float设置为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; } } }