ipad开发之---图片擦除效果

45 篇文章 0 订阅
43 篇文章 0 订阅
使用CoreGraphices框架实现的图片擦除效果。如下:

//
// EraseImageView.h
// Eraser
//
// Created by scott.8an@gmail.com on 11-11-7.
// Copyright 2011 LittleWorn. All rights reserved.
//

#import <UIKit/UIKit.h>
@interface EraseImageView : UIImageView {
@private
UIImageView *foregroundImageView;
BOOL canErase;
UIImage *backgroundImage;
UIImage *foregroundImage;
}
//init
-(id)initWithFrame:(CGRect)frame
backgroundImage:(UIImage*)bgImage
foregroundImage:(UIImage*)fgImage;
@end



//
// EraseImageView.m
// Eraser
//
// Created by scott.8an@gmail.com on 11-11-7.
// Copyright 2011 LittleWorn. All rights reserved.
//

#import "EraseImageView.h"

@implementation EraseImageView

#pragma mark life cycle
- (void)dealloc {
[super dealloc];
}

- (id)initWithFrame:(CGRect)frame backgroundImage:(UIImage*)bgImage foregroundImage:(UIImage*)fgImage{
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
self.userInteractionEnabled = YES;
self.image = bgImage;

foregroundImageView = [[UIImageView alloc] initWithFrame:frame];
foregroundImageView.userInteractionEnabled = YES;
[foregroundImageView setImage:fgImage];
[self addSubview:foregroundImageView];
[foregroundImageView release];
}
return self;
}

#pragma mark override
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if ([touch view] == foregroundImageView){
canErase = YES;
}
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if (canErase) {
CGPoint currentPoint = [touch locationInView:foregroundImageView];
UIGraphicsBeginImageContext(foregroundImageView.frame.size);
[foregroundImageView.image drawInRect:foregroundImageView.bounds];
CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(currentPoint.x, currentPoint.y, 30, 30));
foregroundImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
canErase = NO;
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值