2016-08-12 139 views
0

介紹調整CGPath大小

我有一個CGPath我從SVG文件中使用PocketSVG API創建。它一切正常。

的問題

的問題是,形狀綿延出於某種原因,請把這張圖片上看看(藍色是隻是爲了讓對你更加明顯,請忽略它,它應該是ClearColor):

Image 1 的目標

我想要什麼來實現呢?我想要實現一個遍佈屏幕寬度的形狀(我不關心高度,它應該根據寬度自行修改),並粘貼到屏幕的底部,請看看這張圖片爲阱(請忽略圓形按鈕):

Image 2

守則

最重要的部分;)

我有UIView一個子類,從SVG文件繪製這種形狀,它叫CategoriesBarView。然後,在我的MainViewControllerUIViewController的一個子類)上,我創建了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變量。縮放比例爲1.0,這意味着它將繪製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:使用scaleWidth = 1.0和scaleHeight = 1表示100x100平方。0 Represents 100x100 square using scaleWidth = 1.0 and scaleHeight = 1.0

圖2:表示在使用scaleWidth = 0.50和scaleHeight 50x75平方= 0.75 Represents 100x100 square using scaleWidth = 0.50 and scaleHeight = 0.75

注意:在給定的圖像的所有附圖中UIView- (void)drawRect:(CGRect)rect方法完成的,因爲只有UIView能夠畫畫。我已經放置了一張UIView,它在圖像中用GrayColor高亮顯示。

我相信它給你一個關於縮放路徑來解決你的問題的觀點,因爲你不能使用相同的代碼,但你可以使用它來生成一個代碼。

有用的工具:如果你不是圖形編碼方面的專家,你可以推薦使用PaintCode software,它使用UI生成Objective-C代碼。以爲可能有其他軟件可以選擇。

快樂編碼:)