ios的刮刮卡效果

时间:2015-04-16 06:42:51

标签: ios scratchcard

我正在尝试为iOS制作刮刮卡类型的应用程序 我最初可以用手指擦拭图像 但是我想在图像完全消失后立即发出警报。

我使用以下代码擦除:

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

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:self.view];
}

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

    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];

    UIGraphicsBeginImageContext(self.view.frame.size);

    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.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());

    self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();

    [self.tempDrawImage setAlpha:opacity];

    UIGraphicsEndImageContext();



    lastPoint = currentPoint;

    UIGraphicsBeginImageContext(self.view.frame.size);


}

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

    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.view.frame.size);
        [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.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());
        self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

    UIGraphicsBeginImageContext(self.mainImage.frame.size);
    [self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity];
    self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
    pixelImage=UIGraphicsGetImageFromCurrentImageContext();
    self.tempDrawImage.image = nil;
    UIGraphicsEndImageContext();
}

1 个答案:

答案 0 :(得分:0)

您从第三方ios-scratch-n-see图书馆获得了想法。

这是一个Objective-C类,可以帮助创建类似于刮刮卡的遮罩效果,当在屏幕上滑动时,用户可以在滑动时看到显示的图像。这是一种很酷的效果,可用于刮刮卡动作或擦拭玻璃。

希望这有用!

相关问题