Iphone UITouch上UIView中的擦除功能

Iphone UITouch上UIView中的擦除功能,iphone,ios,Iphone,Ios,我有两个UIView,一个在另一个之上,其中第一个是红色,作为backgroundColor,上一个是白色,作为backgroundColor,当用户触摸并着色上一个UIView时,下一个UIView必须可见。。这里可能吗…?是的,这是可能的,我在我的项目中也做过。看看这个 希望这能帮助您完成您想做的事情;-) 我为您的这一要求提供了类代码。。试着理解一下流程 创建一个UIView类,如bellow 请参阅下面的TouchView.h文件 // // TouchView.h // //

我有两个
UIView
,一个在另一个之上,其中第一个是红色,作为
backgroundColor
,上一个是白色,作为
backgroundColor
,当用户触摸并着色上一个
UIView
时,下一个
UIView
必须可见。。这里可能吗…?

是的,这是可能的,我在我的项目中也做过。看看这个


希望这能帮助您完成您想做的事情;-)

我为您的这一要求提供了类代码。。试着理解一下流程

创建一个UIView类,如bellow

请参阅下面的
TouchView.h
文件

   //
//  TouchView.h
//
//  Created by Paras Joshi on 23/08/12..
//

#import <UIKit/UIKit.h>

@interface TouchView : UIView {

    // array of all the touch locations
    NSMutableArray *points;

    // image to mask on user gesture
    UIImage *maskImage;

}
@property (nonatomic, retain) UIImage *maskImage;
@property (nonatomic, retain) NSMutableArray *points;
-(void)drawPic:(UIImage *)thisPic;
@end
 //  TouchView.m
//
//  Created by Paras Joshi on 23/08/12..

#import "TouchView.h"

// Define method POINT to return the CGPoint value of the object at self.points[index] 

#define POINT(index)    [[self.points objectAtIndex:index] CGPointValue];




@interface TouchView (PrivateMethods)

    -(void)clipImage;

    @end


    @implementation TouchView
    @synthesize points,maskImage;

    // mask will be a sphere of maskWidth X maskHeight
    float maskWidth = 30.0f;
    float maskHeight = 30.0f;


    - (id)initWithFrame:(CGRect)frame {

        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code.
            self.backgroundColor = [UIColor clearColor];
            self.points = [NSMutableArray array];

            // set the 'maskImage'
            maskImage = [[UIImage alloc]init];
    //      maskImage = [UIImage imageNamed:@"house1.jpeg"];

            // listen for 'reset' notification event
    //      [[NSNotificationCenter defaultCenter] addObserver:self
    //                                               selector:@selector(reset:)
    //                                                   name:ResetNotification
    //                                                 object:nil];

        }
        return self;
    }

    -(void)drawPic:(UIImage *)thisPic {

        maskImage = thisPic;

        [maskImage retain];
        [self setNeedsDisplay];
    }

    -(void)reset:(NSNotification *)notification{

        [self.points removeAllObjects];
        [self setNeedsDisplay];

    }
    -(void)drawRect:(CGRect)rect{

        // break if there are no points to mask
        if (!self.points || self.points.count < 2) return; 

        // mask the image
        [self clipImage];
    }

    // clipImage handles creating and adding a path of masks (ellipses), to our drawing context (display) based on gesture input
    - (void) clipImage{

        // grab a reference to the image we're going to mask
        UIImage *image = maskImage;

        // determine the extents of the image so that we do not 
        // do unnecessary drawing ouside the image bounds
    //  CGRect bounds = CGRectMake(0.0f, 0.0f, maskImage.size.width, maskImage.size.height);
        CGRect bounds = CGRectMake(0.0f, 0.0f, 1024, 694);  
        // get the graphics context and init a path to draw our mask to
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGMutablePathRef path = CGPathCreateMutable();

        // add ellipses to every CGPoint in self.points
        for(int i = 0; i < self.points.count - 1; i++){

            // get the CGPoint value stored in self.points[i]
            CGPoint pt = POINT(i);

            // add the ellipse at that point
            CGPathAddEllipseInRect(path, NULL, CGRectMake(pt.x - (maskWidth / 2), pt.y - (maskHeight / 2), maskWidth, maskHeight));
        }

        // add the path of ellipses to our drawing context
        CGContextAddPath(context, path);

        // clip the Core Graphics drawing context to our display
        CGContextClip(context);

        // now add the image to our display (it will only appear wherever the CGPoints of self.points are located)
        [image drawInRect:bounds];

        // finalize drawing
        CGContextClosePath(context);

    }


    - (void)dealloc {
        points = nil;

        // remove event listener
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [super dealloc];
    }


    #pragma mark -
    #pragma mark Gesture Methods

    -(BOOL) isMultipleTouchEnabled{ return NO; };       

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

        // get the location of the touch
        CGPoint pt = [[touches anyObject] locationInView:self];

        // puch the touch location (pt) onto our array of points
        [self.points addObject:[NSValue valueWithCGPoint:pt]];

        // update display by calling it's 'drawRect' method
        [self setNeedsDisplay];

    }

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

        // get the location of the touch
        CGPoint pt = [[touches anyObject] locationInView:self];

        // puch the touch location (pt) onto our array of points
        [self.points addObject:[NSValue valueWithCGPoint:pt]];

        // update display by calling it's 'drawRect' method
        [self setNeedsDisplay]; 


    }
    @end
只需添加此视图作为主视图的子视图,如下面所示

首先在.h文件中添加类

@class TouchView;
然后在
.h
文件中创建如下对象

TouchView *viewScratch;
并在
.m
文件中添加此代码

 - (void)viewDidLoad
   {
     viewScratch = [[TouchView alloc]initWithFrame:CGRectMake(0, 0, 1024, 694)];// set frame which you want...
    [viewScratch clipsToBounds];
    [viewScratch drawPic:[UIImage imageNamed:@"yourImageName"]];
    [self.view addSubview:viewScratch];
   }

把这个问题改写一下。。现在还不清楚您想要什么-还可以添加一些您尝试过的代码,等等on@HarishSaran看到这个链接也为那个家伙,我希望这能帮助你:)谢谢沙阿…投票。。