2017-02-23 31 views
0

我有一个UIImageView,你可以点击它画一个圆。我将圆圈的位置存储在字典数组中。这允许我“重放”圆圈的绘图。但是,当UIImageView与原始大小不同时,圆形不会缩放到新的UIImageView。如何缩放CAShapeLayer到不同的UIImageView

如何获得圈子的比例?出于演示目的,最上面的图片是用于输入的UIImageView的大小,第二张是重放的大小。

输入查询的圆圈:

enter image description here

重播的圆圈(圆圈应该是在蓝色的UIImageView

enter image description here

import Foundation 
import UIKit 

class DrawPuck { 

    func drawPuck(circle: CGPoint, circleColour: CGColor, circleSize: CGFloat, imageView: UIImageView) { 

     let circleBezierPath = UIBezierPath(arcCenter: CGPoint(x: circle.x,y: circle.y), radius: CGFloat(circleSize), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true) 

     let shapeLayer = CAShapeLayer() 

     shapeLayer.path = circleBezierPath.cgPath 

     //change the fill color 
     shapeLayer.fillColor = circleColour 

     //you can change the stroke color 
     shapeLayer.strokeColor = UIColor.white.cgColor 

     //you can change the line width 
     shapeLayer.lineWidth = 0.5 

     imageView.layer.addSublayer(shapeLayer) 

    } 

} 

回答

0

我能够与CATransform3DMakeScale作为解决此只要保持原始图像的原始高宽比,它就可以很好地工作

let width = yellowImageView.frame.width/blueImageView.frame.width 
    let height = yellowImageView.frame.height/blueImageView.frame.height 

    shapeLayer.transform = CATransform3DMakeScale(height, width, 1.0) 

    view.layer.addSublayer(shapeLayer)