2016-03-21 60 views
0

我希望有一個簽名「繪圖」,當我點擊一個tableviewcell popover。 喜歡這幅畫: enter image description here我想通過單擊tableviewcell出現「繪圖彈出窗口」?

因此,這裏是我的代碼:

- (void)didSelectWithTableView:(UITableView *)tableView controller:(UIViewController *)controller 
{ 
    red = 0.0/255.0; 
    green = 0.0/255.0; 
    blue = 0.0/255.0; 
    brush = 2.0; 
    opacity = 1.0; 

    viewController = [[UIViewController alloc] init]; 

    mainImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewController.view.frame.size.width, viewController.view.frame.size.height)]; 
    mainImage.backgroundColor = [UIColor whiteColor]; 
    tempDrawImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewController.view.frame.size.width, viewController.view.frame.size.height)]; 
    tempDrawImage.backgroundColor = [UIColor whiteColor]; 
    [viewController.view addSubview:mainImage]; 
    [mainImage setContentMode:UIViewContentModeScaleToFill]; 
    [viewController.view addSubview:tempDrawImage]; 
    [tempDrawImage setContentMode:UIViewContentModeScaleToFill]; 

    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [saveBtn addTarget:self 
       action:@selector(save) 
     forControlEvents:UIControlEventTouchUpInside]; 
    [saveBtn setTitle:@"Save" forState:UIControlStateNormal]; 
    [saveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    NSLog(@"frame : %@", viewController.view); 
    saveBtn.frame = CGRectMake(viewController.view.frame.origin.x + 570, viewController.view.frame.origin.y + 20, 60.0, 80.0); 
    [viewController.view addSubview:saveBtn]; 

    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [cancelBtn addTarget:self 
        action:@selector(reset) 
     forControlEvents:UIControlEventTouchUpInside]; 
    [cancelBtn setTitle:@"Reset" forState:UIControlStateNormal]; 
    [cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 
    NSLog(@"frame : %@", viewController.view); 
    cancelBtn.frame = CGRectMake(viewController.view.frame.origin.x + 20, viewController.view.frame.origin.y + 20, 60.0, 80.0); 
    [viewController.view addSubview:cancelBtn]; 

    SignaturePopover = [[UIPopoverController alloc] initWithContentViewController:viewController]; 
    SignaturePopover.delegate = self; 
    SignaturePopover.popoverContentSize = CGSizeMake(644, 425); //your custom size. 

    [SignaturePopover presentPopoverFromRect:self.contentView.frame inView:self.contentView permittedArrowDirections: UIPopoverArrowDirectionDown | UIPopoverArrowDirectionUp animated:YES]; 
} 

的問題是,當我點擊tableviewcell,這個功能也堪稱TableViewCell,但他們當我點擊popover時不會被調用:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesBegan:touches withEvent:event]; 

    [self.nextResponder touchesBegan:touches withEvent:event]; 
    NSLog(@"viewController : %@", self.class); 
    if (![self.class isSubclassOfClass: [FXFormSignatureCell class]]) 
    { 
     mouseSwiped = NO; 
     UITouch *touch = [touches anyObject]; 
     lastPoint = [touch locationInView:viewController.view]; 
    } 
} 

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

    [self.nextResponder touchesMoved:touches withEvent:event]; 
     mouseSwiped = YES; 
     UITouch *touch = [touches anyObject]; 
     CGPoint currentPoint = [touch locationInView:viewController.view]; 

     UIGraphicsBeginImageContext(viewController.view.frame.size); 
     [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, viewController.view.frame.size.width, viewController.view.frame.size.height)]; 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); 
     CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); 

     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     [self.tempDrawImage setAlpha:opacity]; 
     UIGraphicsEndImageContext(); 

     lastPoint = currentPoint; 
    // } 
} 

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

    [self.nextResponder touchesEnded:touches withEvent:event]; 
     if(!mouseSwiped) 
     { 
      UIGraphicsBeginImageContext(viewController.view.frame.size); 
      [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, viewController.view.frame.size.width, viewController.view.frame.size.height)]; 
      CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
      CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity); 
      CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
      CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
      CGContextStrokePath(UIGraphicsGetCurrentContext()); 
      CGContextFlush(UIGraphicsGetCurrentContext()); 
      self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
      UIGraphicsEndImageContext(); 
     } 

     UIGraphicsBeginImageContext(self.mainImage.frame.size); 
     [self.mainImage.image drawInRect:CGRectMake(0, 0, viewController.view.frame.size.width, viewController.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0]; 
     [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, viewController.view.frame.size.width, viewController.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity]; 
     self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     self.tempDrawImage.image = nil; 
     UIGraphicsEndImageContext(); 
     NSLog(@"mainimage : %@, tempimage : %@", mainImage, tempDrawImage); 
} 

Q uestion:如何在popover subView中調用touchesBegan,touchesMoved和touchesEnded?提前致謝:D

回答

1

觸摸方法不被調用,因爲Popover是一個單獨的視圖控制器。你的touchesBegan和相關的方法是錯誤的地方。

爲了使所有的工作,只需創建一個新的UIViewController子類,說SignatureViewController

然後更換您的第一行:中SignatureViewController.m

viewController = [[UIViewController alloc] init]; 

viewController = [[SignatureViewController alloc] init]; 

和實現觸摸方法

+0

非常感謝你維諾德,你救了我的一些編碼小時,今晚;) – Claudio

相關問題