2017-09-07 71 views
0

Three Controllers only first two pressed無法同時繪製到多個子類UIView對象。試過很多方法

[編輯]

原來的問題BELOW開始。這裏是更多的信息

我一直在這個問題上整天工作,無法找到一個解決方案。我已經創建了一個UIView子類,並將繪圖代碼剪下來。

-(void)drawRect:(CGRect)rect 
{ 


    //// rechargeLevel Drawing 
    CGRect rechargeLevelRect = CGRectMake(17.5, 17.75, 64, 64); 
    UIBezierPath* rechargeLevelPath = [UIBezierPath bezierPath]; 
    [rechargeLevelPath addArcWithCenter: CGPointMake(CGRectGetMidX(rechargeLevelRect), CGRectGetMidY(rechargeLevelRect)) radius: rechargeLevelRect.size.width/2 startAngle: 90 * M_PI/180 endAngle: endAngle * M_PI/180 clockwise: YES]; 

    [[UIColor redColor] setStroke]; 
    rechargeLevelPath.lineWidth = 4.5; 
    [rechargeLevelPath stroke]; 

    endAngle++; 
    if (endAngle > 359) { 
     endAngle = 1; 
    } 
} 

我試過以下所有的東西。

  1. 的drawRect:與調用setNeedsDisplay計時器(也試過setNeedsDisplayInRect:

  2. CAShapeLayer與調用繪製的形狀的被稱爲後塊內

  3. CAShapeLayer的方法的計時器因爲我在想這個計時器正在搞砸東西。

  4. 創建了UIView的第二個子類,它的代碼都是相同的,但是使用了diff不同的標籤。初始化其中的一個仍然不起作用。

  5. 我試過兩個子類之間的上述方法的組合,但仍然沒有。

當您使用相同的上下文啓動另一個或其全部時,計時器會停止。我不知道。

我已經創建了一個GitHub存儲庫和我所做的測試項目。它有一個viewController,它有六個按鈕,從兩個子類中初始化上述每個方法,並將它們添加到視圖中。

的GitHub庫是:https://github.com/MoseCode/drawTest

[結束編輯]

原來的問題如下:

試圖做定製的drawRect在同一時間的計時器到多個對象繪製。

我有一個自定義UIController(WP_BonusController)設置爲接收觸摸事件。在觸摸控制器5次後,它將禁用事件,直到經過指定的時間。

爲了說明充電時間,我從DrawRect繪製了一個圓形路徑。我正在使用UIGraphicsGetCurrentContext,然後撫摸新的路徑。

我的問題是,當我實例化一堆這些WP_BonusController對象,他們都從UIGraphicsGetCurrentContext共享相同的上下文。我檢查了對象的ram分配,它們都是不同的。但是當我在drawRect中放置一個斷點:並從UIGraphicsGetCurrentContext中檢查CGContextRef時,它們都具有相同的RAM分配。

當我點擊bonusController1五次計時器啓動並開始在圖標周圍繪製圓圈。它工作正常。

然後我按下bonusController2五次,它啓動它的計時器,但是當它在drawRect中繪製上下文時,我從另一個bonusController1獲得相同的上下文。所以繪製的線已經是一半了。

這對任何數量的這些控制器都是一樣的。

有沒有一種方法可以創建我自己的不使用UIGraphicsGetCurrentContext的乾淨上下文。我一直在尋找,找不到解決方案。也許我沒有正確理解上下文的用法。我需要能夠在任何時間和多次同時單獨繪製這些控制器。

這裏是我的drawRect:即獲得由定時器叫:

-(void)drawRect:(CGRect)rect 
{ 
    _currentAngle = _currentAngle + 1; 

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.frame.size.width, self.frame.size.height), NO, 0); 


    // General Declarations 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    // Resize to Target Frame 
    CGContextSaveGState(context); 
    CGRect resizedFrame = WP_CustomIconsResizingBehaviorApply(WP_CustomIconsResizingBehaviorAspectFill, CGRectMake(0, 0, 100, 100), [self getImageRect]); 
    CGContextTranslateCTM(context, resizedFrame.origin.x, resizedFrame.origin.y); 
    CGContextScaleCTM(context, resizedFrame.size.width/100, resizedFrame.size.height/100); 


    //// rechargeLevel Drawing 
    CGRect rechargeLevelRect = CGRectMake(17.5, 17.75, 64, 64); 
    UIBezierPath* rechargeLevelPath = [UIBezierPath bezierPath]; 
    [rechargeLevelPath addArcWithCenter: CGPointMake(CGRectGetMidX(rechargeLevelRect), CGRectGetMidY(rechargeLevelRect)) radius: rechargeLevelRect.size.width/2 startAngle: 90 * M_PI/180 endAngle: _currentAngle * M_PI/180 clockwise: YES]; 

    [WP_CustomIcons.thirdColor setStroke]; 
    rechargeLevelPath.lineWidth = 4.5; 
    [rechargeLevelPath stroke]; 
    CGContextRestoreGState(context); 

    refillMeter = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    [self.refillImageView setImage:refillMeter]; 

} 
+1

這場平局方法屬於一個自定義的UIView實現? –

+0

是的Reinier,它是一種自定義的UIView。它是UIControl的一個子類,我認爲它是UIView的一個子類,並認爲它應該可以。我花了一天的時間嘗試不同的實現,並沒有得到任何工作。我要用我試過的東西來更新我的問題。謝謝。 –

+0

由於Irma颶風,我很抱歉自從星期五以來連接到SO,我發現你可以處理歡呼! –

回答

0

想通了。最後一個嘗試。也許應該是第一次,但是我只是因爲某些原因想着定時器。

CALayerAnimation

@implementation ThirdTimerView 


- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // 
    } 
    return self; 
} 

-(UIBezierPath *)samplePath 
{ 
    CGRect rechargeLevelRect = CGRectMake(17.5, 17.75, 64, 64); 
    UIBezierPath* rechargeLevelPath = [UIBezierPath bezierPath]; 
    [rechargeLevelPath addArcWithCenter: CGPointMake(CGRectGetMidX(rechargeLevelRect), CGRectGetMidY(rechargeLevelRect)) radius: rechargeLevelRect.size.width/2 startAngle: 90 * M_PI/180 endAngle: 450 * M_PI/180 clockwise: YES]; 

    return rechargeLevelPath; 
} 


- (void)startAnimation 
{ 
    if (self.pathLayer == nil) 
    { 
     CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 

     shapeLayer.path = [[self samplePath] CGPath]; 
     shapeLayer.strokeColor = [[UIColor redColor] CGColor]; 
     shapeLayer.fillColor = nil; 
     shapeLayer.lineWidth = 4.5f; 
     shapeLayer.lineJoin = kCALineJoinBevel; 

     [self.layer addSublayer:shapeLayer]; 

     self.pathLayer = shapeLayer; 
    } 

    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeTimer"]; 
    pathAnimation.duration = 3.0; 
    pathAnimation.fromValue = @(0.0f); 
    pathAnimation.toValue = @(1.0f); 

    [CATransaction setCompletionBlock:^{ 

     [self removeFromSuperview]; 

    }]; 

    [self.pathLayer addAnimation:pathAnimation forKey:@"strokeTimer"]; 
} 

@end