2016-08-12 5 views
0

소개CGPath 크기 조정

내가 가지고 CGPath 내가 PocketSVG API를 사용하여 SVG 파일에서 만들 수 있습니다. 그것은 모두 잘 작동합니다.

문제점

문제는 모양이 어떤 이유로 뻗어 있다는 것입니다, 청색은 그것을 무시하세요, 그것은해야 만 할 것입니다 당신에게 더 볼 수 있습니다 (이 그림에서 봐 주시기 바랍니다 수 ClearColor) :

Image 1

는 내가 달성하고자하는 않는 대상? 나는 화면의 너비에 걸쳐가는 모양을 얻고 싶다. (나는 높이를 신경 쓰지 않고 너비에 맞게 수정해야한다.) 그리고 화면 하단에 붙어있다.이 그림을 다음과 같이 보아라. 도 (원형 버튼을 무시하세요) :

Image 2

강령

중요한 부분, 나는 SVG 파일에서이 모양을 그립니다 UIView의 서브 클래스가

) 그것은 0을 불렀다.. 그런 다음 내 MainViewController (하위 클래스 UIViewController)에 CategoriesBarView의 개체를 만들고이를 프로그래밍 방식으로 하위보기로 설정하고 있습니다.

CategoriesBarView :

class CategoriesBarView: UIView { 
    override func layoutSubviews() { 
     let myPath = PocketSVG.pathFromSVGFileNamed("CategoriesBar").takeUnretainedValue() 

     var transform = CGAffineTransformMakeScale(self.frame.size.width/750.0, self.frame.size.height/1334.0) 
     let transformedPath = CGPathCreateCopyByTransformingPath(myPath, &transform) 

     let myShapeLayer = CAShapeLayer() 
     myShapeLayer.path = transformedPath 

     let blur = UIBlurEffect(style: .Light) 
     let effectView = UIVisualEffectView(effect: blur) 
     effectView.frame.size = self.frame.size 
     effectView.frame.origin = CGPointMake(0, 0) 
     effectView.layer.mask = myShapeLayer 

     self.addSubview(effectView) 

    } 
} 

MainViewController :

override func viewDidLoad() { 
    super.viewDidLoad() 

    let testHeight = UIScreen.mainScreen().bounds.size.height/6 // 1/6 of the screen’s height, that is the height in the target picture approximately, doesn’t it? 

    let categoriesBarView = CategoriesBarView(frame: CGRect(x: 0, y: UIScreen.mainScreen().bounds.size.height - testHeight , width: UIScreen.mainScreen().bounds.size.width, height: testHeight)) 
     categoriesBarView.backgroundColor = UIColor.blueColor() // AS I said, it should be ClearColor 
    self.view.addSubview(categoriesBarView) 
} 

당신의 사람이 문제가 여기 왜 모양이 그렇게 스트레칭 무엇을 알고 있나요? 누군가가 나를 도울 수 있다면 정말 고마워.

은 대단히 감사합니다 :)

+0

화면 해상도에 따라 경로를 조정 해 보았습니까? –

+0

@DipenPanchasara 당신은 무엇을 의미합니까? 어떻게해야합니까? 내 코드에서 무엇을 변경해야합니까? 고맙습니다! –

+0

100x100에 패스를 그리고 320x100에서 동일하게 보이기를 원한다면 패스의 x 포인트에 3.2를 곱하면 새로운 영역은 실제 패스보다 3.2 배 더 길기 때문입니다. 경로의 간단한 확장. –

답변

1

는 100 × 100 크기의 사각형을 그립니다 다음 코드를 고려하십시오. 내가 여기서 한 것은 100x100을 기본 치수로 취한 것입니다 (각각의 비율 또는 비율 치수를 계산하기 쉽기 때문에). 알 수 있듯이 scaleWidthscaleHeight 변수를 정의하여 경로의 현재 축척을 나타냅니다. 크기가 인 순간 100x100의 사각형이 그려지며, 0.50.75으로 각각 변경하면 50X75 픽셀의 사각형이 그려집니다. 눈금 폭과 높이의 차이를 1.00.50.75으로 명확하게 묘사 한 이미지를 참조하십시오.

CGFloat scaleWidth = 0.50f; 
CGFloat scaleHeight = 0.75f; 

//// Square Drawing 
UIBezierPath* bezierSquarePath = [UIBezierPath bezierPath]; 

// ***Starting point of path *** 
[bezierSquarePath moveToPoint: CGPointMake(1, 1)]; 

// *** move to x and y position to draw lines, calculate respective x & y position using scaleWidth & scaleHeight *** 
[bezierSquarePath addLineToPoint: CGPointMake(100*scaleWidth, 1)]; 
[bezierSquarePath addLineToPoint: CGPointMake(100*scaleWidth, 100*scaleHeight)]; 
[bezierSquarePath addLineToPoint: CGPointMake(1, 100*scaleHeight)]; 

// *** end your path *** 
[bezierSquarePath closePath]; 
[UIColor.blackColor setStroke]; 
bezierSquarePath.lineWidth = 1; 
[bezierSquarePath stroke]; 

이미지 1 : 100 × 100 평방 사용 scaleWidth = 1.0 scaleHeight = 1을 나타냅니다.0 Represents 100x100 square using scaleWidth = 1.0 and scaleHeight = 1.0

이미지 2 : 주어진 영상의 모든 도면로만 UIView 가능 UIView- (void)drawRect:(CGRect)rect 방법으로 행해진 다 : scaleWidth = 0.50 scaleHeight을 사용하여 50x75 정사각형 = 0.75 Represents 100x100 square using scaleWidth = 0.50 and scaleHeight = 0.75

참고를 나타냄 그릴. 이미지에 GrayColor로 강조 표시된 UIView을 배치했습니다.

동일한 코드를 사용할 수 없지만이를 사용하여 문제를 해결할 수있는 방법에 대한 관점을 제공한다고 생각합니다.

도구 도움 : 당신이 코딩 그래픽에 전문가가없는 경우 UI와 목표 - C 코드를 생성 PaintCode software을 사용하는 것이 좋습니다 수 있습니다. 당신이 선택할 수있는 다른 소프트웨어가있을 거라고 생각했습니다.

해피 코딩 :

관련 문제