ios - Objective-c setContentOffset 方法

标签 ios objective-c iphone swift

我正在尝试将用 objective-c 编写的代码转换为 Swift 语言。 在以下行中:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

if (scrollView.contentOffset.y < self.mapView.frame.size.height*-1 ) {
    [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x,   self.mapView.frame.size.height*-1)];

}
}

我遇到了这个参数,根据文档,它应该被评估为 bool 类型

self.mapView.frame.size.height*-1

谁能告诉我这里发生了什么以及这个表达式是如何被计算为 bool 值的?指针是我无法将其转换为 Swift 的东西,你能帮助我如何做到这一点吗?

提前致谢

最佳答案

出现此错误的原因是表达式的运算符和对象之间没有空格。

在编译时,编译器会中断此行 self.mapView.frame.size.height* -1。它得到了什么 self.mapView.frame.size.height*-1。所以编译器在这里错误地理解为两个单独的参数。

self.mapView.frame.size.height* 之间需要一个空格来告诉编译器这是一个乘以 self.mapView 的表达式。 frame.size.height-1 值。

所以你的相关 Swift 代码如下:

func scrollViewDidScroll(scrollView: UIScrollView) {
    if (scrollView.contentOffset.y < self.mapView.frame.size.height * -1 ) {
        scrollView .setContentOffset(CGPointMake(scrollView.contentOffset.x, self.mapView.frame.size.height * -1), animated: true)
    }
}

关于ios - Objective-c setContentOffset 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27557180/

相关文章:

ios - Google 登录不在 UIVIewController 中

ios - setNavigationBarHidden 不起作用

ios - 将 __block 参数传递给类方法(用于获取请求)

ios - 表格 View 单元格中的按钮没有响应

ios - 如何调整 UIImageView 的大小(快速)

iphone - [UILabel setImage :]: unrecognized selector sent to instance

objective-c - AVAudioSessionCategoryPlayAndRecord 使 AirPlay 不可见

c++ - Obj-C++ 头文件中的 AppKit 对象?

ios - 我的 TableView 中的数据一次又一次地重新加载?

ios - iOS 10支持从XCode 7.x创建ipa并提交到AppStore吗?