【问题标题】:Only Play videos on visible cell [duplicate]仅在可见单元格上播放视频[重复]
【发布时间】:2016-04-06 15:08:00
【问题描述】:

我怎样才能只播放可见单元格的视频? 一旦单元格不可见就停止视频?

我知道这与 indexPathsForVisibleRows 有关,但在确定如何实现它时遇到问题

当前一次可见单元格。

.......

import UIKit
import AVKit
import AVFoundation

class videoDevTable: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 3
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! VideoTableViewCell

    let videoURL = NSURL(string: "https://scontent.cdninstagram.com/t50.2886-16/12951809_1538642766431783_2068695559_n.mp4")
    let player = AVPlayer(URL: videoURL!)

    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = cell.playerView.bounds

    cell.playerView.layer.addSublayer(playerLayer)
    player.play()

    return cell
}

【问题讨论】:

  • 你的权利。我需要在单元格不显示后清除单元格,并且只在视图中播放单元格

标签: ios swift uitableview avplayer


【解决方案1】:

我开发了类似的东西,将view(带视频)添加到自定义单元格并在tableView的-didScroll代表上调用-updateVisibility方法

假设self 这里是您对视频的看法

    - (void)updateVisibilityInScrollView:(UIScrollView*)scrollView
    {
        if (!self.superview) {
            return;
        }
    // Get rect of video relative to scrollView (which is tableView)
        CGRect relativeToScrollViewRect = [self convertRect:self.bounds toView:scrollView];
// get visible scrollView rect
        CGRect visibleScrollViewRect = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, scrollView.bounds.size.width, scrollView.bounds.size.height);

        if ([self moreThenHalfOfRect:relativeToScrollViewRect visibleInRect:visibleScrollViewRect]) {
            // visible half - resume
        } else {
            // supposed to be paused
        }
    }


- (BOOL)moreThenHalfOfRect:(CGRect)rect visibleInRect:(CGRect)visibleRect
{
    return (CGRectContainsPoint(visibleRect, CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))));
}

Ofcaurce,您可以修改以显示完全可见单元格的视频

抱歉,我刚刚复制了我的解决方案(没有 swift 示例)。

【讨论】:

  • func updateVisibility(in scrollView: UIScrollView) { if !superview { return } let relativeToScrollViewRect: CGRect = convert(bounds, to: scrollView) let visibleScrollViewRect = CGRect(x: scrollView.contentOffset.x, y : scrollView.contentOffset.y, 宽度: scrollView.bounds.size.width, 高度: scrollView.bounds.size.height) if moreThenHalfOf(relativeToScrollViewRect, visibleIn: visibleScrollViewRect) { } else { }} func moreThenHalfOf(_ rect: CGRect, visibleIn visibleRect: CGRect) -> Bool {return visibleRect.contains(CGPoint(x: rect.midX, y: rect.midY)) }
猜你喜欢
  • 1970-01-01
  • 2020-04-06
  • 1970-01-01
  • 2014-02-05
  • 2019-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多