用手指在UIImageView上用滚动视图绘图

时间:2011-05-06 18:15:52

标签: objective-c ipad uiscrollview

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

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

·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 个答案:

答案 0 :(得分:0)

我认为你必须将你的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];