2016-05-14 48 views
0

我有一個自定義的UIView,裏面我必須展示一個圖像作爲背景並且使用觸摸和拖拽自由地繪製它,引用這個問題iOS:iOS: Drawing line is appearing behind subviews, 但是沒有' Ť似乎是工作,它繪製的圖像,但在不畫任何東西,請大家幫忙不能在UIView裏面繪製UIImage

以下自定義的UIView繪製放開手腳

- (id)initWithCoder:(NSCoder *)aDecoder // (1) 
{ 
    if (self = [super initWithCoder:aDecoder]) 
    { 
     [self setMultipleTouchEnabled:NO]; // (2) 
     [self setBackgroundColor:[UIColor whiteColor]]; 
     path = [UIBezierPath bezierPath]; 
     [path setLineWidth:2.0]; 
    } 


    return self; 
} 

- (void)drawRect:(CGRect)rect // (5) 
{ 

    [[UIColor blackColor] setStroke]; 
    [path stroke]; 

} 


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:self]; 
    [path moveToPoint:p]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:self]; 
    [path addLineToPoint:p]; // (4) 
    [self setNeedsDisplay]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self touchesMoved:touches withEvent:event]; 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self touchesEnded:touches withEvent:event]; 
} 


@end 

這是主要的UIView裏面我已經加入的UIImage和OverlayView的

- (id)initWithCoder:(NSCoder *)aDecoder // (1) 
{ 
    if (self = [super initWithCoder:aDecoder]) 
    { 
     CGRect rect1 = self.frame; 
     rect1.origin.x = 0; 
     rect1.origin.y = 0; 

     UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(rect1.origin.x,rect1.origin.y,rect1.size.width,rect1.size.height)]; 
     dot.image = [UIImage imageNamed:@"miter"]; 
     [self addSubview:dot]; 

     OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(rect1.origin.x,rect1.origin.y,rect1.size.width,rect1.size.height)]; 
     [overlay setBackgroundColor:[UIColor clearColor]]; 

     [self addSubview:overlay]; 
     [self bringSubviewToFront:overlay]; 

    } 


    return self; 
} 



@end 
+0

你還沒有描述如何這是行不通的,你得到的線,它在哪裏,等... – Wain

+0

@wain它不作出任何線的時候我在屏幕上滑動手指,圖像出現在背景,但沒有任何東西可以畫出來 –

+1

你的觸摸方法被稱爲? – Wain

回答

0

將userInteractionEnabled屬性設置爲YES。 UIViews的默認值是YES,但是對於UIImageViews是NO,所以你必須明確地打開它。