Find the overlapping area of two imageviews overlap, and make this area change colour

自古美人都是妖i 提交于 2019-12-24 12:21:35

问题


I'd like to be able to drag one imageview onto another, such that when they overlap, the overlapping area changes colour. To help visualise the problem, these imageviews are circular: when they overlap, it should form a Venn diagram-style image as shown below.

I know you can detect whether the intersection has occurred by using:

if (self.imageview1.bounds.contains(self.imageview2.bounds)) {
    ...
}

But do not really know how to colour the area in between!


回答1:


so I figured out a way to do this mathematically! Basically you use basic trigonometry to find the angles required for the two "arcs" of the intersection, and make a Bezier Path composed of these two arcs. Then just superpose the bezier path and you're done! Here's my code if anyone is interested :)

Note: this assumes both circles have the same radius, and I have clipped my imageviews such that they are circular!

let circlePath = UIBezierPath()
let left_circle_center = left_image_view.center.x
let right_circle_center = right_image_view.center.x
let radius = left_image_view.frame.width/2
let angle = acos( (left_circle_center - right_circle_center)/radius) 
intersection_Path.addArc(withCenter: right_circle_center, radius: radius, startAngle: CGFloat(Double.pi-angle), endAngle: CGFloat(Double.pi+angle), clockwise: true)
intersection_Path.addArc(withCenter: left_circle_center, radius: radius, startAngle: CGFloat(-angle), endAngle: CGFloat(angle), clockwise: true               
let intersection_area = CAShapeLayer()
intersection_area.path = intersection_Path.cgPath
intersection_area.borderColor = ...
intersection_area.strokeColor = ...
intersection_area.fillColor = ...
self.view.layer.addSublayer(intersection_area)


来源:https://stackoverflow.com/questions/46361119/find-the-overlapping-area-of-two-imageviews-overlap-and-make-this-area-change-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!