在Swift中的ios-MIN()和MAX(),并将Int转换为CGFloat
发布时间:2020-12-14 06:05:19 所属栏目:百科 来源:网络整理
导读:我得到一些错误与以下方法: 1)如何返回screenHeight / cellCount作为第一种方法的CGFLoat? 2)如何在第二种方法中使用等价的ObjC的MIN()和MAX()? func tableView(tableView: UITableView!,heightForRowAtIndexPath indexPath: NSIndexPath!) - CGFloat { v
我得到一些错误与以下方法:
1)如何返回screenHeight / cellCount作为第一种方法的CGFLoat? 2)如何在第二种方法中使用等价的ObjC的MIN()和MAX()? func tableView(tableView: UITableView!,heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat { var cellCount = Int(self.tableView.numberOfRowsInSection(indexPath.section)) return screenHeight / cellCount as CGFloat } // #pragma mark - UIScrollViewDelegate func scrollViewDidScroll(scrollView: UIScrollView) { let height = CGFloat(scrollView.bounds.size.height) let position = CGFloat(MAX(scrollView.contentOffset.y,0.0)) let percent = CGFloat(MIN(position / height,1.0)) blurredImageView.alpha = percent }
1:你不能从Int下降到CGFloat。您必须使用Int作为输入来初始化CGFloat。
return CGFloat(screenHeight) / CGFloat(cellCount) 2:使用标准库定义的最小和最大函数。它们的定义如下: func min<T : Comparable>(x: T,y: T,rest: T...) -> T func max<T : Comparable>(x: T,rest: T...) -> T 用法如下。 let lower = min(17,42) // 17 let upper = max(17,42) // 42 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |