1

以下のコードを使用して、指の動きで PNG をストロークしました。2 つの UIImage ビューがあります。背景画像をそこに置くために、背景に配置します。もう1つは、その上にPNG画像をストロークする明確なUIImageビューです。

  -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
   {
       for (UITouch * touch in touches) {

          currentPoint = [touch locationInView:self.view];
          lastPoint = [touch previousLocationInView:self.view];

    //set up array to make space between PNG images
          if (ABS(currentPoint.x-lastPoint.x)>16
               || ABS(currentPoint.y - lastPoint.y) > 13) {

              [brushLocations addObject:[NSValue valueWithCGPoint:currentPoint]];


      }
        [self drawingWithArray];

 }

  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

         [brushLocations removeAllObjects];//reset


     }


 -(void)drawingWithArray{



     UIGraphicsBeginImageContext(self.view.frame.size);
     [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width,    drawImage.frame.size.height)];

     for (int i=0; i<[brushLocations count]; i++) {

 CGPoint center =[[brushLocations objectAtIndex:i]CGPointValue];


    // bokehImage is UIImage 

         bokehImage=[bokehImgArray objectAtIndex: i%[bokehImgArray count]];

 /// the PNG images are not semi-transparent, even set the alpha is 0.5??

         [bokehImage drawAtPoint:center blendMode:kCGBlendModeOverlay alpha:0.5f];

//drawImage is uiimage view on top of background image view for stroke PNG images.
     drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();

}

今、私は応答が遅いという問題があります。デバイス (iPad4) で指を動かしている間、PNG 画像がすぐに表示されませんでした。

また、PNG 画像は半透明ではありません。drawAtPoint .. blendMode .. alpha の機能で画像を半透明(0.5 alpha を設定)にできると思います。

4

1 に答える 1

0

はい、次のようなものが動作するはずです:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch * touch in touches) {
        currentPoint = [touch locationInView:self.view];
        lastPoint = [touch previousLocationInView:self.view];
        //set up array to make space between PNG images
        if (ABS(currentPoint.x-lastPoint.x)>16
            || ABS(currentPoint.y - lastPoint.y) > 13) {
            [brushLocations addObject:[NSValue valueWithCGPoint:currentPoint]];
        }
//        [self drawingWithArray]; // don't call draw routine during touch handler
         [self setNeedsDisplay]; // queue the redraw instead
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// Not needed here
//    [brushLocations removeAllObjects];//reset
}

//-(void)drawingWithArray
- (void)drawRect:(CGRect)rect
{
    // CGContext is already set when drawRect is called
//    UIGraphicsBeginImageContext(self.view.frame.size);
//    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
    [drawImage.image drawInRect:rect];
    for (int i=0; i<[brushLocations count]; i++) {
        CGPoint center =[[brushLocations objectAtIndex:i]CGPointValue];
        // bokehImage is UIImage
        bokehImage=[bokehImgArray objectAtIndex: i%[bokehImgArray count]];
        // the PNG images are not semi-transparent, even set the alpha is 0.5??
        [bokehImage drawAtPoint:center blendMode:kCGBlendModeOverlay alpha:0.5f];
        //drawImage is uiimage view on top of background image view for stroke PNG images.
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
//        UIGraphicsEndImageContext();
    }
    [brushLocations removeAllObjects];//reset
}
于 2013-11-04T17:12:33.947 に答える