VuframeLab / PanicAR

Augmented Reality Framework for iOS – PanicAR will be sunset April 30th, 2017
http://panicar.vuframe.com
207 stars 61 forks source link

Adding a custom subview to a PARPoi object #30

Closed perryao closed 9 years ago

perryao commented 9 years ago

I'm trying to add a custom view to a PARPoi. Specifically, I'd like to add an FLAnimatedImageView from Flipboards FLAnimatedImage library. I've subclassed PARPoi and overrode renderInView, but this seems to just fill the entire screen with the image with no regard to the given coordinates. How can I add a subview to a PARPoi and ensure that the subview tracks the movement of the device? Thanks! Below is my subclassed PARPoi.

class ARLabel: PARPoi {
    var gifPath: String?
    var animatedImageView: FLAnimatedImageView?
    init(pathToGif: String?) {
        super.init()
        gifPath = pathToGif
    }

    override func addedToARController() {
        println("Added to ARControlller called")
    }

    override func renderInView(theView: UIView!) {
        println("render in view called")
        if(animatedImageView == nil) {
            var animatedImage = FLAnimatedImage(animatedGIFData: NSData(contentsOfFile: self.gifPath!))
            animatedImageView = FLAnimatedImageView()
            animatedImageView!.animatedImage = animatedImage
            animatedImageView!.frame = theView.frame
            theView.addSubview(animatedImageView!)
        }
    }
}
Miiha commented 9 years ago

Hi,

there should be a way: You should subclass from PARPoiLabel, not from PARPoi and to apply your custom UI you might copy the nib file DefaultPoiLabel.xib into your project and add elements there.

perryao commented 9 years ago

Hi, Thanks for the tip. Unfortunately, I think this is something I need to be able to set in code, as the FLAnimatedImage library uses FLAnimatedImage objects for gifs and this class does not subclass UIImage. If I pass in my gif like so:

var gif = PARPoiLabel(title: "Gif building", theDescription: "A Gif", theImage: UIImage(named: "building.gif"), fromTemplateXib: "PoiLabelWithImage", atLocation: CLLocation(latitude:23.56, longitude: -82.34));

then the image is rendered statically without animating.

perryao commented 9 years ago

Ok I figured it out. Instead of adding the subview in the subclassed PARPoiLabel, I added the subview in the view controller after instantiating the PARPoiLabel. Here's the code:

func createARObjects() {
        PARController.sharedARController().clearObjects()
        var gif = PARPoiLabel(title: "", theDescription: "", theImage: nil, fromTemplateXib: "PoiLabelWithImage", atLocation: CLLocation(latitude:23.56, longitude:-82.34));

        var string = NSBundle.mainBundle().pathForResource("building", ofType: "gif")
        var data = NSData(contentsOfFile: string!)
        var animatedImage = FLAnimatedImage(animatedGIFData:data)
        var animatedImageView = FLAnimatedImageView()
        animatedImageView.animatedImage = animatedImage
        animatedImageView.frame = gif.labelView.frame
        gif.labelView.addSubview(animatedImageView)
        gif.offset = CGPointMake(0, 0)
        PARController.sharedARController().addObject(gif)

    }

So I think it's safe to close this. Thanks for the help!