简体   繁体   English

Objective-C setContentOffset方法

[英]Objective-c setContentOffset method

I am trying to convert code written in objective-c into Swift language. 我正在尝试将用Objective-C编写的代码转换为Swift语言。 In following lines: 在以下几行中:

- (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)];

}
}

I encountered this parameter which should be, due to documentation, evaluated to boolean type 我遇到此参数,由于文档原因,应该将该参数评估为布尔类型

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

Can anybody tell me what's going on here and how this expression is evaluated to boolean? 谁能告诉我这是怎么回事,以及该表达式如何评估为布尔值? the pointer is the thing that unables me to convert it into Swift, could you help how to do so? 指针是无法让我将其转换为Swift的东西,您能帮忙怎么做吗?

Thanks in advance 提前致谢

The reason for this error is because of no space between operator and object for an expression. 出现此错误的原因是因为表达式的运算符和对象之间没有空格。

On compilation time compiler breaks this lines self.mapView.frame.size.height* -1 . 在编译时,编译器会中断这行self.mapView.frame.size.height* -1 And what it gets self.mapView.frame.size.height* and -1 . 以及得到的self.mapView.frame.size.height*-1 So compiler mis understand as two separate parameters here. 因此,编译器在这里将其理解为两个单独的参数。

A space between self.mapView.frame.size.height and * required to tell the compiler that this is an expression to multiply self.mapView.frame.size.height with -1 value. self.mapView.frame.size.height*之间self.mapView.frame.size.height一个空格,以告诉编译器这是一个将self.mapView.frame.size.height-1值相乘的表达式。

So your relevant Swift code will be as below: 因此,您相关的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)
    }
}
self.mapView.frame.size.height * -1

这是一个乘法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM