修改属性可以调用一个方法吗?

时间:2021-12-27 11:17:05

Can someone explain me why in iOS

有人能解释为什么在iOS里吗?

 var dx = (touchPoint.x - dragTouch.x) * self.transform.a
 var dy = (touchPoint.y - dragTouch.y) * self.transform.d

 self.transform.tx = self.transform.tx + dx 
 self.transform.ty = self.transform.ty + dy

 if ( self.frame.origin.y > 70){ // lock upper edge during drag
       self.frame.origin.y = 70
 }

is different from

不同于

 var dx = (touchPoint.x - dragTouch.x) * self.transform.a
 var dy = (touchPoint.y - dragTouch.y) * self.transform.d

 if ( self.frame.origin.y > 70){ // lock upper edge during drag
       self.frame.origin.y = 70
 }

 self.transform.tx = self.transform.tx + dx 
 self.transform.ty = self.transform.ty + dy

It seems that modifying a property causes a method to be called, and consequently a redraw of the view. If i use the first solution, the edge of my view is locked, otherwise, using the second piece of code, i obtain a different behaviour.

似乎修改属性会导致调用一个方法,从而重新绘制视图。如果我使用第一个解决方案,我的视图的边缘被锁定,否则,使用第二段代码,我就会得到不同的行为。

Do you know how i can avoid this problem?

你知道我怎样才能避免这个问题吗?

2 个解决方案

#1


0  

As @HotLicks kind of adressed there is probably stuff going on in the background. Since we don't know the implementation details it is hard to say with certainty.

正如@HotLicks所做的那样,在后台可能会发生一些事情。由于我们不知道具体的实现细节,所以很难确定。

  • Option #1 would be what @HotLicks said:
    In that scenario there's a private property, which you are only able to access via a get and/or set within a different, public computed property.
  • 选项#1是@HotLicks说的:在这种情况下,有一个私有属性,您只能通过get和/或设置在不同的公共计算属性中访问。
  • Option #2 would be almost the same:
    It is possible that the property has willSet and/or didSet property observers. This would be a different approach to the same principle as in option #1.
  • 选项#2几乎是相同的:属性有可能设置和/或didSet属性观察器。这将是与选择#1中相同原则的不同方法。

You probably can't avoid this. But you probably should not try to either. There's usually a reason why the implementation is as it is. And having a seperate get/set or property observers is a method of constraining your (the programmer's) actions in a way to suit to the given framework.

你可能无法避免。但你也不应该尝试。实现的原因通常是这样的。拥有一个seperate get/set或属性观察器是一种约束您(程序员)行为的方法,以适应给定的框架。

#2


0  

Thank you, i guess i solved the problem. The documentation says that every time a frame property is modified, methods like setNeedsUpdateConstraints() are called, and that causes a layout redraw. I solved avoiding to modify directly frame properties, modifying only dx and dy.

谢谢,我想我解决了这个问题。文档说明每次修改帧属性时,都会调用setneedsupdateconstr()方法,这会导致布局重绘。我解决了避免直接修改框架属性,只修改dx和dy。

#1


0  

As @HotLicks kind of adressed there is probably stuff going on in the background. Since we don't know the implementation details it is hard to say with certainty.

正如@HotLicks所做的那样,在后台可能会发生一些事情。由于我们不知道具体的实现细节,所以很难确定。

  • Option #1 would be what @HotLicks said:
    In that scenario there's a private property, which you are only able to access via a get and/or set within a different, public computed property.
  • 选项#1是@HotLicks说的:在这种情况下,有一个私有属性,您只能通过get和/或设置在不同的公共计算属性中访问。
  • Option #2 would be almost the same:
    It is possible that the property has willSet and/or didSet property observers. This would be a different approach to the same principle as in option #1.
  • 选项#2几乎是相同的:属性有可能设置和/或didSet属性观察器。这将是与选择#1中相同原则的不同方法。

You probably can't avoid this. But you probably should not try to either. There's usually a reason why the implementation is as it is. And having a seperate get/set or property observers is a method of constraining your (the programmer's) actions in a way to suit to the given framework.

你可能无法避免。但你也不应该尝试。实现的原因通常是这样的。拥有一个seperate get/set或属性观察器是一种约束您(程序员)行为的方法,以适应给定的框架。

#2


0  

Thank you, i guess i solved the problem. The documentation says that every time a frame property is modified, methods like setNeedsUpdateConstraints() are called, and that causes a layout redraw. I solved avoiding to modify directly frame properties, modifying only dx and dy.

谢谢,我想我解决了这个问题。文档说明每次修改帧属性时,都会调用setneedsupdateconstr()方法,这会导致布局重绘。我解决了避免直接修改框架属性,只修改dx和dy。