ios – How one can Make Popover Dialog Transfer with AR Mannequin?

0
21
ios – How one can Make Popover Dialog Transfer with AR Mannequin?


How one can Make Popover Dialog Transfer with AR Mannequin?

I am engaged on an AR plugin. I’ve applied a characteristic the place a pop-up seems when part of the AR mannequin is tapped. After I click on on a particular half, I name the operate:

showInfoPopup(partName: partName, targetPoint: location, modelTitle: partName)
On this operate, I take advantage of the next code to generate a pop-up:
popOver = PopOver(targetPoint: targetPoint, topEdgeY: topEdgeY, leftEdgeX: leftEdgeX, rightEdgeX: rightEdgeX, bottomEdgeY: bottomEdgeY, title: title, content material: content material)

if isPopupAllowed == true {
    if let popOver = popOver {
        arView.addSubview(popOver)
    }
    isPopupAllowed = false
    print("add popover isn't allowed")
}

The PopOver is a customized UIView class (code is offered on the finish of this submit).

Drawback:
I would like my pop-up dialog to maneuver together with the AR mannequin as I transfer my cellphone. I’ve tried a few approaches to replace the pop-up place, however they solely make the pop-up flicker briefly after the faucet, after which it disappears.

Makes an attempt:

if let popOver = popOver {
    popOver.isHidden = false
    let deltaX = screenPoint.x - CGFloat(targetPoint.x)
    let deltaY = screenPoint.y - CGFloat(targetPoint.y)
    popOver.remodel = CGAffineTransform(translationX: deltaX, y: deltaY)
}
if let popOver = popOver {
    var newFrame = popOver.body
    let deltaX = screenPoint.x - CGFloat(targetPoint.x)
    let deltaY = screenPoint.y - CGFloat(targetPoint.y)
    newFrame.origin.x += deltaX
    newFrame.origin.y += deltaY
    popOver.body = newFrame
}

The consequence for each approaches is that the popover simply sparkles as soon as after tapping after which disappears.

Render Loop and Updating Popover Place:
Right here is the framework of my render loop and the way I replace the popover place:

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
    DispatchQueue.primary.async {
        self.updateOverlayPosition()
        self.updatePopOver(xRatio: self.xRatio, yRatio: self.yRatio, targetPoint: self.targetPoint)
    }
}

@objc func updatePopOver(xRatio: Float, yRatio: Float, targetPoint: CGPoint) {
    guard let modelNode = modelNode else { return }
    
    // Get mannequin's bounding field and calculate the middle level
    let min = modelNode.boundingBox.min
    let max = modelNode.boundingBox.max
    let targetPoint = SCNVector3(
        (min.x + max.x) / xRatio,
        (min.y + max.y) / yRatio,
        (min.z + max.z) / 2
    )
    
    // Convert the middle level to world coordinates
    let worldTarget = modelNode.convertPosition(targetPoint, to: nil)
    
    // Convert the world coordinates to display screen coordinates
    let projectedPoint = arView.projectPoint(worldTarget)
    
    // If the coordinate is on-screen, replace the popover's place
    if projectedPoint.z > 0 {
        let screenPoint = CGPoint(x: CGFloat(projectedPoint.x), y: CGFloat(projectedPoint.y))
        popOver?.isHidden = false
        // TODO: replace popover's place
    } else {
        popOver?.isHidden = true
    }
}

PopOver Class:
Beneath is the PopOver class, which inherits from UIView:

import UIKit

class PopOver: UIView {
    // Initialization technique with customizable content material and magnificence
    init(targetPoint: CGPoint,
         topEdgeY: CGFloat,
         leftEdgeX: CGFloat,
         rightEdgeX: CGFloat,
         bottomEdgeY: CGFloat,
         lineLength: CGFloat = 200,
         circleRadius: CGFloat = 5.0,
         cornerRadius: CGFloat = 20.0,
         title: String,
         content material: String,
         titleFont: UIFont = UIFont.boldSystemFont(ofSize: 25),
         contentFont: UIFont = UIFont.systemFont(ofSize: 15),
         titleColor: UIColor = UIColor(hex: "#6A6E74"),
         contentColor: UIColor = UIColor(hex: "#6A6E74"),
         lineColor: UIColor = .white,
         backgroundColor: UIColor = .white,
         borderColor: UIColor = .lightGray) {
        
        tremendous.init(body: .zero)
        self.body = UIScreen.primary.bounds
        setupDialogueBox(targetPoint: targetPoint,
                         topEdgeY: topEdgeY,
                         leftEdgeX: leftEdgeX,
                         rightEdgeX: rightEdgeX,
                         bottomEdgeY: bottomEdgeY,
                         lineLength: lineLength,
                         circleRadius: circleRadius,
                         cornerRadius: cornerRadius,
                         title: title,
                         content material: content material,
                         titleFont: titleFont,
                         contentFont: contentFont,
                         titleColor: titleColor,
                         contentColor: contentColor,
                         lineColor: lineColor,
                         backgroundColor: backgroundColor,
                         borderColor: borderColor)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been applied")
    }
    
    // (Extra implementation particulars...)

    personal func setupDialogueBox(...) {
        // Implementation to arrange the popover field
    }
}

Query:
How can I make my pop-up transfer along with the AR mannequin because the digicam strikes?

The present situation is that the pop-up sparkles and disappears. I would like the pop-up to at all times be on the appropriate place relative to the mannequin when the cellphone strikes.

Would recognize any recommendation or course!

LEAVE A REPLY

Please enter your comment!
Please enter your name here