2015-09-18 83 views
2

有很多Watch應用程序,它們的WKInterfaceImages具有圓角。我試圖在我的測試應用程序中甚至將一些WKInterfaceImages舍入,但我無法理解如何做到這一點。如何在Watch應用程序中對WKInterfaceImage的角落進行四捨五入?

我無法使用imageView.layer。 ...與正常的iPhone應用程序一樣,我無法找到使用代碼或故事板的替代方案。

我一定要屏蔽所有的PNG或有更簡單的方法?

回答

1

您是對的CALayerUIView不能直接在watchOS 2上使用。但是您可以使用圖形功能,例如this approach完全可以在Watch上使用。

在斯威夫特的模擬:

class ImageTools { 
    class func imageWithRoundedCornerSize(cornerRadius:CGFloat, usingImage original: UIImage) -> UIImage { 
     let frame = CGRectMake(0, 0, original.size.width, original.size.height) 

     // Begin a new image that will be the new image with the rounded corners 
     UIGraphicsBeginImageContextWithOptions(original.size, false, 1.0) 

     // Add a clip before drawing anything, in the shape of an rounded rect 
     UIBezierPath(roundedRect: frame, cornerRadius: cornerRadius).addClip() 

     // Draw the new image 
     original.drawInRect(frame) 

     // Get the new image 
     let roundedImage = UIGraphicsGetImageFromCurrentImageContext() 

     // Lets forget about that we were drawing 
     UIGraphicsEndImageContext() 

     return roundedImage 
    } 
} 

在某處,WKInterfaceController類:

let originalImage = UIImage(named: "original-image")! 
let roundedImage = ImageTools.imageWithRoundedCornerSize(60, usingImage: originalImage) 

// Set `UIImage` for your `WKInterfaceImage` 
imageOutlet.setImage(roundedImage) 
+0

@ Matte.Car - 我更新了一個更適合Watch的鏈接。 –

+0

它甚至可以工作,但它在Objective-C中...我試着翻譯它,但我不能... –

+0

@ Matte.Car - 在Swift中添加了示例。它完美的作品。 –

1

我解決了從故事板卸下WKInterfaceImage然後我設置與以前相同尺寸的WKInterfaceGroup替換它然後圖像,從屬性檢查員,我設置他的半徑(是的,有可能的團體!)然後我宣佈組在控制器中,並設置圖像使用row.flagView.setBackgroundImageNamed(imageName)

+0

其實我知道它:)但認爲這個問題是關於WKInterfaceImage的。無論如何,我很高興聽到你解決了你的問題! –

+0

是啊,我的這是一個騙局!如果有人找到問題的真實解決方案,我會保持這個問題的開放。 –