1

How can I draw a straight line between two UIImageViews and have the line "refresh" if one of the views is moved. I assume I'll have to use NSNotificationCenter, but other then that I'm a bit stuck.

Thanks for your help!!!

Matt S.
  • 13,305
  • 15
  • 73
  • 129

1 Answers1

1

A good way to do this would be to use KVO (Key-Value Obverving).

If you use KVO to watch for changes in the bounds (or frame) of the views, you will get notified about position changes, at which point you can (re-)render a line on the display.

More about KVO: https://stackoverflow.com/questions/1470167/is-there-any-tutorial-out-there-on-key-value-coding-and-key-value-observing

As for rendering a line: one often used technique is to write a subclass of UIView in which you override the drawRect method and draw a line using Core Graphics. See this question: How do I draw a line on the iPhone?

Community
  • 1
  • 1
occulus
  • 16,959
  • 6
  • 53
  • 76
  • I'll give you an upvote for the line one, but I'm not sure if KVO is the right thing for me to use, but I'll take a closer look. – Matt S. Mar 08 '11 at 01:51
  • 1
    Thanks. Depending on what exactly you're doing with the views (i.e. how they get moved around), the KVO method can work quite nicely. For example, if you use UIView animations to animate the views onto, or around the screen, the KVO technique would keep the line drawing up to the date with the moving views. Just drawing a line based on user touch input would fail in that case. – occulus Mar 08 '11 at 10:35