大佬教程收集整理的这篇文章主要介绍了ios – 使用watchOS 2在Apple Watch上渲染折线图,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
以下是我使用的代码:
import WatchKit import Foundation import UIKit class InterfaceController: WKInterfaceController{ override func awakeWithContext(context: AnyObject?) { super.awakeWithContext(context) let path = UIBezierPath() let startPoint = CGPointMake(0.0,0.0) path.moveToPoint(startPoint) let nextPoint = CGPointMake(20.0,20.0) path.addLineToPoint(nextPoint) path.lineWidth = 1.0 UIColor.whiteColor().setstroke() path.stroke() } override func willActivate() { super.willActivate() } override func didDeactivate() { super.didDeactivate() } }
我的最终结果应该像Apple Watch上的Stocks app.每当用户点击特定股票时,他将能够查看/可视化该股票的统计数据.任何人都可以帮助我实现这一目标.
>创建基于位图的图形上下文,并使用UIGraphicsBeginImageContext将其设置为当前上下文.
>借鉴上下文.
>从上下文中提取CGImageRef并将其转换为UIImage对象.
>在WKInterfaceGroup或WKInterfaceImage上显示图像.
码:
// Create a graphics context let size = CGSizeMake(100,100) UIGraphicsBeginImageContext(sizE) let context = UIGraphicsGetCurrentContext() // Setup for the path appearance CGContextSetstrokeColorWithColor(context,UIColor.whiteColor().CGColor) CGContextSetLineWidth(context,4.0) // Draw lines CGContextBeginPath (context); CGContextMoveToPoint(context,0); CGContextAddLineToPoint(context,100,100); CGContextMoveToPoint(context,100); CGContextAddLineToPoint(context,0); CGContextstrokePath(context); // Convert to UIImage let cgimage = CGBitmapContextCreateImage(context); let uiimage = UIImage(CGImage: cgimage!) // End the graphics context UIGraphicsEndImageContext() // Show on WKInterfaceImage image.setImage(uiimagE)
image是WKInterfaceImage属性.这个对我有用.
我也可以在watchOS上使用UIBezierPath绘制如下:
// Create a graphics context let size = CGSizeMake(100,100) UIGraphicsBeginImageContext(sizE) let context = UIGraphicsGetCurrentContext() UIGraphicsPushContext(context!) // Setup for the path appearance UIColor.greenColor().setstroke() UIColor.whiteColor().setFill() // Draw an oval let rect = CGRectMake(2,2,96,96) let path = UIBezierPath(ovalInRect: rect) path.lineWidth = 4.0 path.fill() path.stroke() // Convert to UIImage let cgimage = CGBitmapContextCreateImage(context); let uiimage = UIImage(CGImage: cgimage!) // End the graphics context UIGraphicsPopContext() UIGraphicsEndImageContext() image.setImage(uiimagE)
以上是大佬教程为你收集整理的ios – 使用watchOS 2在Apple Watch上渲染折线图全部内容,希望文章能够帮你解决ios – 使用watchOS 2在Apple Watch上渲染折线图所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。