iOS编程4THE EDITION in page:112 touchesBegan :( NSSet *)触摸无法响应

时间:2016-07-02 10:46:09

标签: ios

1)方法-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event未得到回复。
2)关于画一个同心圆的应用
3)Xcode 7.3.1

#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

1 个答案:

答案 0 :(得分:1)

确保您的视图的UserInteractionEnabled为true。

[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];
}