`
re_reference
  • 浏览: 233894 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多
使用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
1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics