-1

In my program, I have a View called "previewView" applied to be an area to show the camera capturing window. I am trying to apply another View based on the information that the "previewView" shows by using script instead of dragging a new view from the storyboard, and the View/frame in red should be at the same position as the "previewView". Now, I am getting a wrong result like the picture shown below.

The wrong display

The code I wrote for implementing the function is here: let targetRegion = CALayer()

func addTargetLayer(){
    //try to convert the position of
    //the "previewView" to the absolute position of screen
    var tempPreviewView = previewView.convertPoint(previewView.center, toView: nil)

    //apply the position converted previously
    //to the targetRegion in the red colour
    targetRegion.frame = CGRect(origin: CGPoint(x: tempPreviewView.x, y: tempPreviewView.y), size: CGSize(width: previewView.frame.width, height: previewView.frame.height))

    let color = UIColor.redColor()
    targetRegion.backgroundColor=color.CGColor
    targetRegion.opacity = 0.5
    self.view.layer.addSublayer(targetRegion)


}

I also tried something like the answer suggested in the following post, but it didn't work for me: Using convertPoint to get the relative position inside a parent UIView

Anyone have some ideas? Thanks

Community
  • 1
  • 1
Ares Li
  • 575
  • 1
  • 6
  • 17

1 Answers1

0

I figured it out. It works after I deleted all the constrains. But next question came to my mind. How could I set the constrains copied from other frame to the frame created by using script?

Ares Li
  • 575
  • 1
  • 6
  • 17