Objc bindings and gesture recognition

This creates a draggable UIView, with some issues:

– Objc Views

function setup()
–watch variables to be set during gesture recognition
parameter.watch(“touchOnDragView”)
parameter.watch(“touchOnIOSScreen”)

--make the view to drag
dvFrame = {
    x = WIDTH/2, 
    y = HEIGHT/2, 
    width = 400, 
    height = 400
}
dragView = objc.UIView()
--objective-c y zero is the top of the screen, the opposite of Codea 
dragView.frame = objc.rect(dvFrame.x, HEIGHT - dvFrame.y - dvFrame.height, dvFrame.width, dvFrame.height)
dragView.backgroundColor = color(225, 188, 215)

--add view to screen view
objc.viewer.view:addSubview_(dragView)

--make object to recieve updates from the gesture recognizer
local GestureHandler = objc.class("GestureHandler")
function GestureHandler:dragDragView_(sender)
    --get touch x, y relative to screen and to view
    local screenLoc = dragRecognizer:locationInView_()
    local viewLoc = dragRecognizer:locationInView_(dragView)
    --update the variables for the watch parameters
    touchOnDragView = vec2(viewLoc.x, viewLoc.y)
    touchOnIOSScreen = vec2(screenLoc.x, screenLoc.y)
    --calculate correct place for the new x, y
    local adjX = math.floor(math.abs(screenLoc.x + viewLoc.x))
    local adjY = math.floor(math.abs(screenLoc.y + viewLoc.y))
    --update frame
    dragView.frame = objc.rect(adjX, adjY, dvFrame.width, dvFrame.height)
end
local gHandler = GestureHandler()

--make gesture recognizer and setup object to receive updates
dragRecognizer = objc.UIPanGestureRecognizer()
dragRecognizer:addTarget_action_(gHandler, objc.selector("dragDragView:"))
dragView:addGestureRecognizer_(dragRecognizer)

end

function draw()
background(224, 220, 183)
end

Issue 1: the view pops around on the screen as you drag it.
Issue 2: drag it for long enough and Codea crashes

@jfperusse can you give guidance? Are these fixable, or is it just never a good idea to mix iOS touch-processing with Codea?

Hi @UberGoober!

Sorry it’s been a while since you posted this, but here’s a solution using the gesture recognizer’s translation, and resetting it to zero, which gives a smooth dragging functionnality.

function setup()
    --- dragRecognizer: objc.UIPanGestureRecognizer

    --make the view to drag
    dvFrame = {
        x = WIDTH/2, 
        y = HEIGHT/2, 
        width = 400, 
        height = 400
    }

    --- dragView: objc.UIView
    dragView = objc.UIView()
    --objective-c y zero is the top of the screen, the opposite of Codea 
    dragView.frame = objc.rect(dvFrame.x, HEIGHT - dvFrame.y - dvFrame.height, dvFrame.width, dvFrame.height)
    dragView.backgroundColor = color(225, 188, 215)

    --add view to screen view
    objc.viewer.view:addSubview_(dragView)

    --make object to recieve updates from the gesture recognizer
    local GestureHandler = objc.class("GestureHandler")
    function GestureHandler:dragDragView_(sender)
        --get and reset the recognizer's translation
        local translation = dragRecognizer:translationInView_(dragView)
        dragRecognizer:setTranslation_inView_(objc.point(0, 0), dragView)

        local frame = dragView.frame

        --calculate correct place for the new x, y
        local adjX = math.floor(frame.origin.x + translation.x)
        local adjY = math.floor(frame.origin.y + translation.y)

        --update frame
        dragView.frame = objc.rect(adjX, adjY, dvFrame.width, dvFrame.height)
    end
    local gHandler = GestureHandler()

    --make gesture recognizer and setup object to receive updates
    dragRecognizer = objc.UIPanGestureRecognizer()
    dragRecognizer:addTarget_action_(gHandler, objc.selector("dragDragView:"))
    dragView:addGestureRecognizer_(dragRecognizer)
end

function draw()
    background(40, 40, 50)
end

@jfperusse - interesting, occasionally stuttered with the drag but generally was pretty smooth.

Found it intriguing when the rectangle was dragable over the top of the parameter/output window. Is objC capable of using it’s own output window ?

Hi @Bri_G !

Yes, with the Obj-C bridge, this basically brings you outside of the Codea “sandbox” as you get access to the core iOS features. This also means you can “break” Codea until the next time you restart it, so you have to be careful :wink:

@jfperusse - Aaaah, does that mean you can interrogate iOS so that you can read the memory allocations granted to Codea ? That would possibly enable us to write a routine to help with memory allocations when coding - that would help us tighten code when needed.

@Bri_G I fear this might not be currently possible as this would require calling global objective-c methods (e.g. os_proc_available_memory, or complex solutions as discussed at xcode - Determining the available amount of RAM on an iOS device - Stack Overflow), and calling such methods is not currently possible. I don’t think we expose any way to get an approximation of the available memory, but we could consider adding it.