objective-c – 在Swift中,你如何编写scrollViewWillEndDragging
发布时间:2020-12-14 17:17:36 所属栏目:百科 来源:网络整理
导读:怎么会在 Swift中写这个? 更新:发布我正在尝试转换的方法,最初发布了一个不同于我试图隐藏的方法 这适用于Objective C,但不适用于Swift.当我尝试将ceilf和floorf与CGFloat一起使用时,我得到了转换错误. - (void)scrollViewWillEndDragging:(UIScrollView *
怎么会在
Swift中写这个?
更新:发布我正在尝试转换的方法,最初发布了一个不同于我试图隐藏的方法 这适用于Objective C,但不适用于Swift.当我尝试将ceilf和floorf与CGFloat一起使用时,我得到了转换错误. - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { float pageWidth = 200 + 30; // width + space float currentOffset = scrollView.contentOffset.x; float targetOffset = targetContentOffset->x; float newTargetOffset = 0; if (targetOffset > currentOffset) newTargetOffset = ceilf(currentOffset / pageWidth) * pageWidth; else newTargetOffset = floorf(currentOffset / pageWidth) * pageWidth; if (newTargetOffset < 0) newTargetOffset = 0; else if (newTargetOffset > scrollView.contentSize.width) newTargetOffset = scrollView.contentSize.width; targetContentOffset->x = currentOffset; [scrollView setContentOffset:CGPointMake(newTargetOffset,0) animated:YES]; } 解决方法
回答Christian:我能够在Swift中重写它.我将所有内容设置为Float来进行数学计算,然后转换为CGFloat,因为Christian提到了可能.他的回答在技术上是正确的,但是向我展示了一个更新的iOS开发人员如何用Swift编写这个是我想要的.
func scrollViewWillEndDragging(scrollView: UIScrollView!,withVelocity velocity: CGPoint,targetContentOffset: UnsafePointer<CGPoint>) { var pageWidth = Float(200 + 30) var currentOffset = Float(scrollView.contentOffset.x) var targetOffset = Float(targetContentOffset.memory.x) var newTargetOffset = Float(0) var scrollViewWidth = Float(scrollView.contentSize.width) if targetOffset > currentOffset { newTargetOffset = ceilf(currentOffset / pageWidth) * pageWidth } else { newTargetOffset = floorf(currentOffset / pageWidth) * pageWidth } if newTargetOffset < 0 { newTargetOffset = 0 } else if newTargetOffset > currentOffset { newTargetOffset = currentOffset } Float(targetContentOffset.memory.x) == currentOffset scrollView.setContentOffset(CGPointMake(CGFloat(newTargetOffset),0),animated: true) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |