Change UIView orientation as it moves along path

Discussion in 'iOS Programming' started by moonman239, Sep 18, 2014.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    As I mentioned in the last thread, I have a race car image view that almost goes around in a circle. For the animation, I am using Core Animation directly. Now that I have done that, I would like to make the image view change its orientation every time its heading is changed. I'm thinking of subclassing UIImageView and overriding its setCenter method so that every time setCenter is called, the app calculates the slope of an imaginary line segment that connects the image view's old center to that of its new center. Using this slope, the image view can then figure out where its corners should be.
     
  2. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    Ok, that didn't work. Evidently, the image view's position property is not updated when its layer moves. I guess I'll have to figure out some other way. I might need to have my view controller do the reorientation.
     
  4. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #4
    OK, using KVO on the "position" property of the layer won't work.
     
  5. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #5
    This does sound like logic that belongs in the ViewController.
     
  6. moonman239, Sep 19, 2014
    Last edited by a moderator: Sep 19, 2014

    moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #6
    I just added the view controller as an observer for the race car image view's layer's presentation layer's bounds. However, I ran the app, but the app was terminated with an error:

    Code:
    instance 0x7a99c510 of class NSConcreteValue was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x7a98b540> (
    <NSKeyValueObservance 0x7a9a3160: Observer: 0x7a993800, Key path: origin, Options: <New: YES, Old: YES, Prior: NO> Context: 0x0, Property: 0x7a993c10>
    )
    2014-09-19 10:12:20.769 Reading Expressway[1346:64701] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x7a99c510 of class NSConcreteValue was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x7a98b540> (
    <NSKeyValueObservance 0x7a9a3160: Observer: 0x7a993800, Key path: origin, Options: <New: YES, Old: YES, Prior: NO> Context: 0x0, Property: 0x7a993c10>
    )'
    
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    Is there a question?

    As the error says, you need to remove any KVO observers before the object is dealloced.

    However, it says the key path is origin and you said it was something else. Not sure what that means.
     
  8. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #8
    OK, I think I found an alternate route. I can create a CALayer, add my image view's layer to it, adjust the image view's layer's position so that it is relative to the other layer, then makes the former layer rotate.
     
  9. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #9
    I found another way - create a view in Interface Builder, put the image view in that view, tell the app to add the newly created view as a subview, and have the app apply a rotation animation to that view's layer.
     

Share This Page