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

设置UIView自动刷新 触摸画图

    博客分类:
  • IOS
 
阅读更多

   

#import "MyView.h"

@implementation MyView
{
    CGContextRef context;//UIGraphicsGetCurrentContext();
    NSMutableArray *array ;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
       
    }
    return self;
}

/**/
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [self drawTab];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if(array == nil){
         array = [[NSMutableArray alloc]initWithCapacity:10];
    }
    
    UITouch *touch = [touches anyObject];
    CGPoint  point = [touch locationInView:self];
    
    [array addObject:[NSValue valueWithCGPoint:point]];
    
    
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint  point = [touch locationInView:self];
    
    [array addObject:[NSValue valueWithCGPoint:point]];
    
    
   
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
     
    UITouch *touch = [touches anyObject];
    CGPoint  point = [touch locationInView:self];
    
    [array addObject:[NSValue valueWithCGPoint:point]];
     [self setNeedsDisplay];
    
}
-(void) drawTab {
    context = UIGraphicsGetCurrentContext();
 
    NSValue *value = nil;
    
    for(int i=0;i<array.count;i++){
        value = [array objectAtIndex:i];
        CGPoint point ;
        [value getValue:&point];
        
        if(i==0){
            CGContextBeginPath(context);
            CGContextMoveToPoint(context,point.x ,point.y);
            
        }else if(i == array.count-1){
        
            CGContextAddLineToPoint(context, point.x, point.y);
            CGContextDrawPath(context, kCGPathStroke);
        }else {
            CGContextAddLineToPoint(context, point.x, point.y);
        
        }
    }

}
@end

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics