2012-10-06 3 views
1

UIView를 사용하고 배경색을 변경할 때 코드가 작동했습니다. 이제 나는 타원으로 그것을 시도하고 그들은 나타나지 않는다? 당신은 touchesMoved:withEvent:에서 UIGraphicsGetCurrentContext 호출iOS에서 원 그리기가 움직였습니다.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
     overlayView = [[[UIView alloc] initWithFrame:self.view.bounds] autorelease]; 
     overlayView.backgroundColor = [UIColor colorWithWhite:255.0f alpha:1.0f]; 
    [self.view addSubview:overlayView]; 
    reda = 0; 
    bluea = 0; 
    greena = 255; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    touchPoint = [touch locationInView:self.view]; 
    previousPoint = touchPoint; 

} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    touchPoint = [touch locationInView:self.view]; 
    if (abs((previousPoint.x-touchPoint.x)) > 20) { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Too Fast!" delegate:self cancelButtonTitle:@"Okay." otherButtonTitles:nil]; 
     [alertView show]; 
     [alertView release]; 
    } 
    CGContextRef contextRef = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBFillColor(contextRef, reda/255.f, greena/255.f, bluea/255.f, 1.0f); 
    CGContextFillEllipseInRect(contextRef, CGRectMake(touchPoint.x, touchPoint.y, 20, 20)); 
    [overlayView addSubview:contextRef]; 

    if ((greena != 0) && (bluea == 0)) { 
     greena += -5; 
     reda += +5; 
    } 
    if ((reda != 0) && (greena == 0)) { 
     bluea += +5; 
     reda += -5; 
    } 
    if ((bluea != 0) && (reda == 0)) { 
     bluea += -5; 
     greena += +5; 
    } 
} 

답변

2

: 여기에 코드입니다. 시스템이 touchesMoved:withEvent:에 그래픽 컨텍스트를 생성하지 않습니다. 시스템은 그래픽 컨텍스트를 -[UIView drawRect:]에 생성합니다.

Apple 설명서에서 view drawing cycleruntime interaction model for views에 대해 읽어야합니다.

당신은 drawRect:에 드로잉 코드를 이동할 수 있습니다, 또는 당신은 당신의 자신의 그래픽 컨텍스트를 생성 할 수 있습니다 (예를 들어, UIGraphicsBeginImageContextWithOptions 또는 CGBitmapContextCreate를 사용하여), 그것에 그린 다음 컨텍스트에서 이미지를 생성하고 view.layer.contents에 이미지를 할당합니다.

+0

Perfect! 위대한 CGBitmapContextCreate 및 UIImage 만들기 및 UIImageView 퍼팅 및 addtosubview 해당보기 추가! –

관련 문제