2016-07-02 8 views
0

1)メソッド-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)eventに応答しません。
2)およそ7.3.1iOSプログラミング4thEdition in page:112 touchesBegan:(NSSet *)タッチが応答できません

#import "BNRHypnosisView.h" 
@interface BNRHypnosisView() 

@property(nonatomic,strong)UIColor *circleColor; 
@end 
@implementation BNRHypnosisView 

-(void)drawRect:(CGRect)rect { 
    CGRect bounds = self.bounds; 
    CGPoint center; 
    center.x = bounds.origin.x + bounds.size.width/2.0; 
    center.y = bounds.origin.y + bounds.size.height/2.0; 
    float maxRadius = hypotf(bounds.size.width, bounds.size.height)/2.0; 
    UIBezierPath *path = [[UIBezierPath alloc]init]; 
     for (float currentRadius = maxRadius; currentRadius > 0 ; currentRadius -=20) { 
     [path moveToPoint:CGPointMake(center.x + currentRadius, center.y)]; 
     [path addArcWithCenter:center radius:currentRadius startAngle:0.0 endAngle:M_PI * 2.0 clockwise:YES]; 
    } 
    path.lineWidth= 5; 
    [self.circleColor setStroke]; 
    [path stroke]; 
} 

- (instancetype)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.circleColor = [UIColor blackColor]; 
     self.backgroundColor = [UIColor whiteColor]; 

      } 
    return self; 
} 

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

    NSLog(@"%@ was touched.",self); 
    float red = (arc4random() % 100) /100.0; 
    float green = (arc4random() % 100) /100.0; 
    float blue = (arc4random() % 100) /100.0; 
    UIColor *radomColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0]; 
    self.circleColor = radomColor; 
} 
-(void)setCricleColor:(UIColor *)circleColor 
{ 
    _circleColor = circleColor; 
    [self setNeedsDisplay]; 
} 
@end 
+0

「 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)」イベントメソッドは、UIViewクラスへのタッチにのみ応答します。 – Bharat

+0

どうすればいいですか? –

+0

'UITapGestureRecognizer'を試してみてください。 – Bharat

答えて

1

UserInteractionEnabledは、あなたのビューのために真であることを確認してください同心円アプリ
3)のXcodeを描画しています。

[self.NameOfBNRHypnosisView setUserInteractionEnabled:true]; 

Also, I found one mistake in your method code. Use setCircleColor instead of setCricleColor. 

-(void)setCircleColor:(UIColor *)circleColor 
{ 
    _circleColor = circleColor; 
    [self setNeedsDisplay]; 
} 
関連する問題