触摸UIImage没有开火

时间:2013-12-26 04:59:16

标签: uiimageview

除了图形只能在视图中工作外,一切正常!我在scrollView上有一个UIImageView。滚动视图是我的键盘将我的项目提升到键盘后面看,但是在我添加scrollView之前图形确实有效,所以我添加了UIImage来在窗体上绘制。现在,如果我将手指从视图拖到UIImageView,它将绘制,而不是从UIImageView开始。我做错了什么......

 - (void)viewDidLoad {
    [super viewDidLoad];
    drawImage = [[UIImageView alloc] initWithImage:nil];
        drawImage.frame = self.view.frame;
    [self.graphicImageView addSubview:drawImage];
    mouseMoved = 0;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:self.graphicImageView];
    lastPoint.y -= 0;

}

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"Touches Moved");

        mouseSwiped = YES;

        UITouch *touch = [touches anyObject];
        CGPoint currentPoint = [touch locationInView:self.graphicImageView];
        currentPoint.y -= 0;

        UIGraphicsBeginImageContext(self.graphicImageView.frame.size);

        [drawImage.image drawInRect:CGRectMake(0, 0, self.graphicImageView.frame.size.width, self.graphicImageView.frame.size.height)];

        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0); //width
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 1.0, 1.0); //color red, green, blue, alpha
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        lastPoint = currentPoint;

        mouseMoved++;

        if (mouseMoved == 10) {
            mouseMoved = 0;
        }
    }


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touches Ended");


    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
       // drawImage.image = nil;
    //    return;
    }


    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.graphicImageView.frame.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, self.graphicImageView.frame.size.width, self.graphicImageView.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0);//width
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 1.0, 1.0);  //color
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

1 个答案:

答案 0 :(得分:0)

好的,UIImageView现在位于scrollView下面。

  • (void)viewDidLoad {

    //键盘 // ---设置滚动视图的可视框架---

    scrollView1.frame = CGRectMake(0,44,359,457);

    // ---设置滚动视图的内容大小---

    [scrollView1 setContentSize:CGSizeMake(359,457)];

    [[NSNotificationCenter defaultCenter] addObserver:self                                          选择:@选择(keyboardWasShown :)                                              name:UIKeyboardDidShowNotification对象:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self                                          选择:@选择(keyboardWillBeHidden :)                                              name:UIKeyboardWillHideNotification对象:nil];

    的NSLog(@ “viewDidLoad中”); drawImage = [[UIImageView alloc] initWithImage:nil]; drawImage.frame = self.view.frame; [self.view addSubview:drawImage];
    mouseMoved = 0;

    self.graphicImageView.userInteractionEnabled = TRUE; 自> scrollView1.userInteractionEnabled = TRUE;

    [super viewDidLoad];

    }

  • (void)touchesBegan:(NSSet *)触及withEvent:(UIEvent *)事件{

    mouseSwiped = NO; UITouch * touch = [触及anyObject];

    lastPoint = [touch locationInView:self.graphicImageView]; lastPoint.y - = 0;

NSLog(@“Touches Began”); }

  • (void)touchesMoved:(NSSet *)触及withEvent:(UIEvent *)event {

    NSLog(@"Touches Moved");
    
    mouseSwiped = YES;
    
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:drawImage];
    currentPoint.y -= 0;
    
    UIGraphicsBeginImageContext(drawImage.frame.size);
    
    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
    
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0); //width
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 1.0, 1.0); //color red, green, blue, alpha
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    lastPoint = currentPoint;
    
    mouseMoved++;
    
    if (mouseMoved == 10) {
        mouseMoved = 0;
    }
    

    }

    • (void)touchesEnded:(NSSet *)触及withEvent:(UIEvent *)event {

    NSLog(@“Touches Ended”);

    UITouch * touch = [触及anyObject];

    if([touch tapCount] == 2){     theSwitch.on = NO;     drawImage.userInteractionEnabled = FALSE;     theSwitchLabel.text = @“Pen is Off”;     doubleTapLabel.hidden = TRUE;     返回; }

    if(!mouseSwiped){     UIGraphicsBeginImageContext(drawImage.frame.size);     [drawImage.image drawInRect:CGRectMake(0,0,drawImage.frame.size.width,drawImage.frame.size.height)];     CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGLineCapRound);     CGContextSetLineWidth(UIGraphicsGetCurrentContext(),1.0); // width     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0.0,0.0,1.0,1.0); //颜色     CGContextMoveToPoint(UIGraphicsGetCurrentContext(),lastPoint.x,lastPoint.y);     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),lastPoint.x,lastPoint.y);     CGContextStrokePath(UIGraphicsGetCurrentContext());     CGContextFlush(UIGraphicsGetCurrentContext());     drawImage.image = UIGraphicsGetImageFromCurrentImageContext();     UIGraphicsEndImageContext();

    } }

    • (IBAction)switchtheswitch:(id)sender {

    if(theSwitch.on){     theSwitchLabel.text = @“Pen is On”;     drawImage.userInteractionEnabled = TRUE;     doubleTapLabel.hidden = FALSE; } 其他{     theSwitchLabel.text = @“Pen is Off”;     drawImage.userInteractionEnabled = FALSE; }

}