【问题标题】:drawing with finger on UIImageView with a scroll View使用滚动视图在 UIImageView 上用手指绘图
【发布时间】:2011-05-06 18:15:51
【问题描述】:

我需要在滚动视图上创建一个 UIImageView,以便触摸。 是一个 100x50 的小 UiimageView,人们可以签名。

我正在使用相同的代码并且它可以工作,但使用滚动视图却不能。 我环顾四周,但找不到代码。

.h

UIImageView *drawImage;

@property (nonatomic, retain) IBOutlet UIImageView *drawImage;

.m

    @synthesize drawImage;

    - (void)viewDidLoad    {
    [scrollView setScrollEnabled:YES];
    [scrollView setContentSize:CGSizeMake(320,1540)];
    if ([MFMailComposeViewController canSendMail])
        button.enabled = YES;
    drawImage.userInteractionEnabled = YES;


    UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchesBegan:)];
    gestureRec.numberOfTouchesRequired = 1;
    gestureRec.numberOfTapsRequired = 1;
    [drawImage addGestureRecognizer:gestureRec];
    [gestureRec release];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(printItem) forControlEvents:UIControlEventTouchDown];
    btn.frame = CGRectMake(75, 0, 44, 44);
   [btn setImage:[UIImage imageNamed:@"print.png"] forState:UIControlStateNormal];

    [self.view addSubview:btn];

}

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

        mouseSwiped = NO;
        UITouch *touch = [touches anyObject];

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

        lastPoint = [touch locationInView:drawImage];
        //  lastPoint.y -=20;// only if signature goes bottom of the screen
    }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        mouseSwiped = YES;

        UITouch *touch = [touches anyObject];   
        CGPoint currentPoint = [touch locationInView:drawImage];
        currentPoint.y -=5; // only if signature goes bottom of the screen


        UIGraphicsBeginImageContext(drawImage.frame.size);

        CGContextRef currentContext = UIGraphicsGetCurrentContext();
        [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
        CGContextSetLineCap(currentContext, kCGLineCapRound);
        CGContextSetLineWidth(currentContext, 2.0);
        CGContextSetRGBStrokeColor(currentContext, 0.0, 0.0, 0.0, 1.0);
        CGContextBeginPath(currentContext);
        CGContextMoveToPoint(currentContext, lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(currentContext, currentPoint.x, currentPoint.y);
        CGContextStrokePath(currentContext);
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        lastPoint = currentPoint;
    }
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

        UITouch *touch = [touches anyObject];

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

        if(!mouseSwiped) {
            UIGraphicsBeginImageContext(drawImage.frame.size);

            CGContextRef currentContext = UIGraphicsGetCurrentContext();
            [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
            CGContextSetLineCap(currentContext, kCGLineCapRound);
            CGContextSetLineWidth(currentContext, 3.0);
            CGContextSetRGBStrokeColor(currentContext, 1.0, 1.0, 2.0, 2.0);
            CGContextMoveToPoint(currentContext, lastPoint.x, lastPoint.y);
            CGContextAddLineToPoint(currentContext, lastPoint.x, lastPoint.y);
            CGContextStrokePath(currentContext);
            drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

            UIGraphicsEndImageContext();
        }
    }

我尝试了 NSZombieEnabled,我在 touchesBegan 的 UITouch 线上得到了一个 SIGABRIT

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

    mouseSwiped = NO;

    UITouch *touch = [touches anyObject];

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

    lastPoint = [touch locationInView:drawImage];
    //  lastPoint.y -=20;// only if signature goes bottom of the screen
}

这里有一个 SIGABRIT,在 int main(int argc, char *argv[]) 行中

    #import <UIKit/UIKit.h>

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

我仍然像以前一样收到错误:

2011-05-10 22:14:22.509 anamnesiprova[90427:207] -[UITapGestureRecognizer anyObject]: unrecognized selector sent to instance 0x6bd5ba0

2011-05-10 22:14:22.512 anamnesiprova[90427:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapGestureRecognizer anyObject]: unrecognized selector sent to instance 0x6bd5ba0'

【问题讨论】:

  • 有人吗?代码有什么问题。谢谢
  • +1 整理,再次感谢。
  • 好的,您可以为您的活动可执行文件启用 NSZombieEnabled 并查看是否出现了新的东西而不是 unrecognized selector sent to instance 0x68adc80 或类似的东西吗?
  • 我试图启用 NSZombieEnabled: product/profile/ 并在环境变量中的参数中我输出 NSZombieEnabled YES,但它不起作用:(,我现在不知道这个 NSZombie 是什么,所以可能我需要以其他方式激活它。再次感谢您的帮助
  • 看看this Q&A 这个tutorial 会帮助你掌握它。

标签: objective-c ipad uiscrollview


【解决方案1】:

我认为您必须将 UIImageView 的 userInteractionEnabled 设置为 YES。

另一个想法是在你的 imageView 中添加一个 GestureRecognizer,首先是一个简单的:

UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myMethodToBeCalled)];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[myImageView addGestureRecognizer:gestureRec];
[gestureRec release];

【讨论】:

  • 感谢 Nick,我将 userInteractionEnable 设置为 YES,但仍然没有来自 UIImageView 的任何响应。
  • @Marco 尝试将 UIScrollView 的 delaysContentTouches 设置为 NO。如果您将 NSLogs 放在您的触摸方法中,它们会被调用吗?我已经为手势识别器添加了一些代码,试试这个。
  • 你真好。我尝试了您的代码,但是一旦我触摸我的 UImageView,应用程序就会崩溃并且我收到此错误:
  • 我尝试了这个动作来检查它是否适用于我的 UImageView: - (void)twoFingersTwoTaps { NSLog(@"Action: tap"); },它给了我正确的动作而不会崩溃。但是当我使用 touchesbegan 操作时它崩溃了。感谢您的帮助
  • 我现在正在尝试 UILongPressGestureRecognizer,所以当你移动手指时它会重复这个动作。我想知道如何做一个每次点击视图时都会产生一个点的动作。 -(void) drawdot: (id) sender {} 有什么帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多