tableViewCell視覺差效果實(shí)現(xiàn)

最近實(shí)現(xiàn)了下tableViewCell視覺差效果,分享一波。

效果圖.gif

原理


圖1.png

<pre>自定義cell,需要一張大于contentView的圖片,并且設(shè)置好相應(yīng)約束。
coverImageView 和 centerYConstraint 作為 IBOutlet。
在tableView滾動(dòng)的時(shí)候通過協(xié)議方法向cell發(fā)送通知,cell收到消息改變constraint實(shí)現(xiàn)效果。</pre>

具體實(shí)現(xiàn)


//需要使用到的成員變量
@IBOutlet weak var tableView: UITableView!    
var dataArray:NSMutableArray = NSMutableArray()
var once:dispatch_once_t = 0
var lastPoint:CGPoint = CGPoint(x: 0, y: 0)
override func viewDidLoad() {
        super.viewDidLoad()
        //設(shè)置代理,往數(shù)據(jù)源加入數(shù)據(jù)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.separatorStyle = .None
        for index in 1...9 {
            let image:UIImage = UIImage(named: "\(index).jpg")!
            self.dataArray.addObject(image);
        }
    }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //注冊(cè)Nib
        dispatch_once(&once) { 
            tableView.registerNib(UINib(nibName: "MyTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "CELL")
        }
        let cell:MyTableViewCell = tableView.dequeueReusableCellWithIdentifier("CELL", forIndexPath: indexPath) as! MyTableViewCell
        return cell
    }

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        //設(shè)置高度
        return 9 / 16 * UIScreen.mainScreen().bounds.size.width
    }


func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        //當(dāng)需要顯示cell時(shí)調(diào)用,填充數(shù)據(jù)
        let cell:MyTableViewCell = cell as! MyTableViewCell
        cell.coverImageView.image = self.dataArray[indexPath.row] as? UIImage
    }
//通過lastPoint.y和contentOffset.y進(jìn)行比較可以得出滑動(dòng)方向,往cell發(fā)送通知
func scrollViewDidScroll(scrollView: UIScrollView) {
        var directionStr:String = ""
        if lastPoint.y > scrollView.contentOffset.y {
            directionStr = "Up"
        } else if lastPoint.y < scrollView.contentOffset.y {
            directionStr = "Down"
        }
        lastPoint = scrollView.contentOffset
        NSNotificationCenter.defaultCenter().postNotificationName("ScrollViewAnimation", object: directionStr)
    }
//cell的成員變量
@IBOutlet weak var coverImageView: UIImageView!
@IBOutlet weak var centerYConstraint: NSLayoutConstraint!
var constantGap:CGFloat = 0.0

override func awakeFromNib() {
        super.awakeFromNib()
        //剪裁掉超出cell的內(nèi)容
        self.contentView.clipsToBounds = true
        self.clipsToBounds = true
        //計(jì)算y軸中心約束可移動(dòng)的最大范圍絕對(duì)值
        constantGap = (2/1) * self.contentView.bounds.size.width / 2 - self.contentView.bounds.size.height/2
        //注冊(cè)通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyTableViewCell.scrollViewAnimation), name: "ScrollViewAnimation", object: nil)
    }

func scrollViewAnimation(notification:NSNotification) {
        let directionStr:String = notification.object as! String
        var constant:CGFloat = self.centerYConstraint.constant
        //通過傳來(lái)的參數(shù)判斷滑動(dòng)方向,實(shí)時(shí)改變約束值
        if directionStr == "Up" {
            if constant < constantGap {
                constant += 1
            } else  if constant > constantGap {
                constant = constantGap
            }
            self.centerYConstraint.constant = constant
        } else if directionStr == "Down" {
            if constant > -1 * constantGap {
                constant -= 1
            } else  if constant < -1 * constantGap {
                constant = -1 * constantGap
            }
            self.centerYConstraint.constant = constant
        }
    }

//移除通知
deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

代碼Demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容