IOS touchesBegan,touchesMoved,touchesEnded,发布多个图像视图

时间:2014-08-07 17:04:19

标签: ios

因此我现在有了代码,有两个图像视图。一个在另一个之上。您在顶部图像上绘制,当触摸结束时,顶部图像将转移到底部图像并保存'。

使用我当前的实现,这只允许在屏幕上绘制一个图像,因为如果我添加另一组图像视图并尝试绘制它,程序将崩溃。

所以我想知道touchesBegan,移动和结束方法是否有办法识别我想要绘制的图像视图集,并绘制适当的图像视图。

//Drawing Functions
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIImageView *drawTemp = (UIImageView *)[self.view viewWithTag:topImageTag];

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:drawTemp];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UIImageView *drawTemp = (UIImageView *)[self.view viewWithTag:topImageTag];

    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:drawTemp];

    UIGraphicsBeginImageContext(drawTemp.frame.size);
    [drawTemp.image drawInRect:CGRectMake(0, 0, drawTemp.frame.size.width, drawTemp.frame.size.height)];
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawTemp.image = UIGraphicsGetImageFromCurrentImageContext();
    [drawTemp setAlpha:opacity];
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIImageView *drawTemp = (UIImageView *)[self.view viewWithTag:topImageTag];
    UIImageView *drawMain = (UIImageView *)[self.view viewWithTag:bottomImageTag];

    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(drawTemp.frame.size);
        [drawTemp.image drawInRect:CGRectMake(0, 0, drawTemp.frame.size.width, drawTemp.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        drawTemp.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

    UIGraphicsBeginImageContext(drawMain.frame.size);
    [drawMain.image drawInRect:CGRectMake(0, 0, drawTemp.frame.size.width, drawTemp.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    [drawTemp.image drawInRect:CGRectMake(0, 0, drawTemp.frame.size.width, drawTemp.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity];
    drawMain.image = UIGraphicsGetImageFromCurrentImageContext();
    drawTemp.image = nil;
    UIGraphicsEndImageContext();
}

任何帮助将不胜感激!

0 个答案:

没有答案
相关问题