2017-04-04 19 views
2

https://www.dropbox.com/s/wlizis5zybsvnfz/File%202017-04-04%2C%201%2052%2024%20PM.jpeg?dl=0如何設置半圓的UIImage在斯威夫特這樣的截圖

您好所有Swifters,

誰能告訴我如何設置這種UI的?他們設置了半圓形圖像嗎?

或者有兩個圖像。一個在背景中的山和anohter圖像在白色背景下半圓形並放置在頂部?

請告知

+0

一種方法是使具有以下使它看起來像它的裁剪透明像素正常正方形圖片,但它是不是:)放入帶有透明背景的uiimageview將會產生與圖片相同的效果。你應該在Photoshop中擁有非常基本的技能來顯示下面的像素 –

+0

您可能想要檢查如何使用Core Graphics繪製弧線。 [this](http://stackoverflow.com/questions/15866179/draw-segments-from-a-circle-or-donut)可能是一個很好的說明。 –

回答

8
  1. 使用UIBezier路徑繪製橢圓形狀。

  2. 繪製一個矩形路徑,它完全類似於保存圖像的imageView。

  3. CGAffineTransform變換橢圓路徑,使它位於矩形路徑的中心。

  4. 將直徑爲CGAffineTransform的矩形軌跡乘以0.5以創建橢圓與矩形的交點。

  5. 使用CAShapeLayer掩蓋圖像。

附加:作爲羅布Mayoff在註釋中規定,你可能需要在viewDidLayoutSubviews計算蒙版尺寸。不要忘記玩它,測試不同的案例(不同的屏幕尺寸,方向),並根據您的需求調整實施。

試試下面的代碼:

import UIKit 

class ViewController: UIViewController { 

@IBOutlet weak var imageView: UIImageView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     guard let image = imageView.image else { 
      return 
     } 

     let size = image.size 

     imageView.clipsToBounds = true 
     imageView.image = image 

     let curveRadius = size.width * 0.010 // Adjust curve of the image view here 
     let invertedRadius = 1.0/curveRadius 

     let rect = CGRect(x: 0, 
          y: -40, 
         width: imageView.bounds.width + size.width * 2 * invertedRadius, 
        height: imageView.bounds.height) 

     let ellipsePath = UIBezierPath(ovalIn: rect) 
     let transform = CGAffineTransform(translationX: -size.width * invertedRadius, y: 0) 
     ellipsePath.apply(transform) 

     let rectanglePath = UIBezierPath(rect: imageView.bounds) 
     rectanglePath.apply(CGAffineTransform(translationX: 0, y: -size.height * 0.5)) 
     ellipsePath.append(rectanglePath) 

     let maskShapeLayer = CAShapeLayer() 
     maskShapeLayer.frame = imageView.bounds 
     maskShapeLayer.path = ellipsePath.cgPath 
     imageView.layer.mask = maskShapeLayer 
    } 
} 

結果:

enter image description here

+0

不要只是發佈代碼。一個好的答案解釋了這個問題和解決方案。 – rmaddy

+1

關於圖層蒙版的一兩句話會有幫助。另外,如果故事板中的視圖與用戶設備的大小不一樣,則這看起來不正確。你需要在'viewDidLayoutSubviews'中計算掩碼路徑。 –

+0

非常感謝@Oleh Zayats – kinchitg

1

爲什麼不能創建(繪製)圓形透明圖像,並在所需高度的視圖頂部添加UIImageView(),並在此視圖下方添加其他視圖。我這是最簡單的方法。我會寫評論,但我不能。