如何在ios中拖动图像

时间:2013-12-20 06:17:01

标签: ios iphone uitouch

我有一个具有图像剪裁功能的应用程序。 功能是我有三张图片:

  • 主图片 - 设置为背景
  • 叠加图像 - 主图像上设置的图像
  • 指针图像 - 将触摸移动的图像

我想在拖动指针图像时移除覆盖图像。剪裁后覆盖图像主图像应该可见。

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    // Retrieve the touch point
    CGPoint pt = [[touches anyObject] locationInView:imgPointer];
    startLocation = pt;
    [[imgPointer superview] bringSubviewToFront:imgPointer];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    // Move relative to the original touch point
    CGPoint pt = [[touches anyObject] locationInView:imgPointer];
    CGRect frame = [imgPointer frame];
    frame.origin.x += pt.x - startLocation.x;
    frame.origin.y += pt.y - startLocation.y;
    [imgPointer setFrame:frame];

    UIGraphicsBeginImageContext(imgPointer.frame.size);

    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGRect clippedRect = CGRectMake(0, 0, imgPointer.frame.size.width, imgPointer.frame.size.height);
    CGContextClipToRect( currentContext, clippedRect);
    CGRect drawRect = CGRectMake(imgPointer.frame.origin.x * -1,
                                 imgPointer.frame.origin.y * -1,
                                 imgOverlay.frame.size.width,
                                 imgOverlay.frame.size.height);

    //draw the image to our clipped context using our offset rect

    CGContextDrawImage(currentContext, drawRect, imgOverlay.image.CGImage);

    //pull the image from our cropped context

    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

}

从使用上面的代码我无法剪辑叠加图像。

谢谢。

0 个答案:

没有答案