2014-10-04 42 views
0

我在我的應用程序中有一個進步圈,可用於3.5和4英寸屏幕,但是我剛開始更新我的iPhone 6和6 Plus應用程序的過程,而我已經注意到進度圈並不按照預期運作。新屏幕尺寸的視圖中心化進度圈

原因是我已經硬編碼了進度圈的位置,以小屏幕爲中心。

我想知道是否有人能告訴我我需要做什麼來轉換它,以便無論屏幕大小,圓圈都居中?

這裏是我的代碼:

class ProgressCircle: UIView { 

override func drawRect(rect: CGRect) { 
    var ctx = UIGraphicsGetCurrentContext() 

    var innerRadiusRatio: CGFloat = 0.6 

    var path: CGMutablePathRef = CGPathCreateMutable() 
    var startAngle: CGFloat = CGFloat(-M_PI_2) 
    var endAngle: CGFloat = CGFloat(-M_PI_2) + min(1.0, progress) * CGFloat(M_PI * 2) 
    var outerRadius: CGFloat = CGRectGetWidth(self.bounds) * 0.5 - 1.0 
    var innerRadius: CGFloat = outerRadius * innerRadiusRatio 
    var center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)) 

    //Code for background circle 

    var context: CGContextRef = UIGraphicsGetCurrentContext() 
    colourTheme = NSUserDefaults.standardUserDefaults().integerForKey("themeSettings") 
    if colourTheme == 1 { 
     CGContextSetFillColorWithColor(context, (UIColorFromRGB(0xEAEAEA)).CGColor) 
    } else { 
     CGContextSetFillColorWithColor(context, (UIColor.darkGrayColor()).CGColor) 
    } 
    var rectangle: CGRect = CGRectMake(0, 0, 171, 171) 
    CGContextBeginPath(context) 
    CGContextAddEllipseInRect(context, rectangle) 
    CGContextDrawPath(context, kCGPathFill) 

    UIGraphicsEndImageContext() 

    if colourTheme == 1 { 
     CGContextSetFillColorWithColor(context, (UIColor.darkGrayColor()).CGColor) 
    } else { 
     CGContextSetFillColorWithColor(context, (UIColorFromRGB(0xEAEAEA)).CGColor) 
    } 
    var rectangleSmall: CGRect = CGRectMake(35.95, 35.95, 99.1, 99.1) //102.6 
    CGContextBeginPath(context) 
    CGContextAddEllipseInRect(context, rectangleSmall) 
    CGContextDrawPath(context, kCGPathFill) 

    UIGraphicsEndImageContext() 

    //Code for progress radius 

    CGPathAddArc(path, nil, center.x, center.y, innerRadius, startAngle, endAngle, false) 
    CGPathAddArc(path, nil, center.x, center.y, outerRadius, endAngle, startAngle, true) 
    CGPathCloseSubpath(path) 
    CGContextAddPath(ctx, path) 

    CGContextSaveGState(ctx) 
    CGContextClip(ctx) 
    if percent > 100 { 
     CGContextDrawImage(ctx, self.bounds, UIImage(named: "RadialProgressFillOver").CGImage) 
    } else { 
     CGContextDrawImage(ctx, self.bounds, UIImage(named: "RadialProgressFill").CGImage) 
    } 
    CGContextRestoreGState(ctx) 
} 
+1

簡單的用自動版式。你是否決定以編程的方式來做這件事? – davidrayowens 2014-10-04 21:57:41

+0

你如何把這個觀點放在屏幕上? – rdelmar 2014-10-04 21:59:38

+0

@dvdowns是的,我試過使用AutoLayout,我真的不喜歡它。我的應用中沒有很多元素是這樣的,所以我決定使用單獨的故事板會是更好的解決方案。 – user3746428 2014-10-04 22:10:49

回答

0

想通了。這實際上很明顯,但我忘記了實際上將進度圈添加到視圖的方式。

這裏是我想出瞭解決方案:

var circleFrame = CGRect(x: (budgetDisplayView.bounds.width/2)-85.5, y: 249, width: 171, height: 171) 
    var circle = ProgressCircle(frame: circleFrame) 
    self.budgetDisplayView.addSubview(circle) 
    self.budgetDisplayView.sendSubviewToBack(circle) 
通過IB