使用UIScrollview缩放/平移图像,将项目放在图像顶部并移动它们

时间:2011-10-18 14:58:14

标签: objective-c

我正在使用uiscrollview显示比屏幕尺寸更大的UIImageview(就像在照片库中查看照片一样)。接下来我需要在用户点击时向屏幕添加一个点(我使用UIImageview显示一个点)。得到了这个。

现在我想要做的就是保持相对于图像的相同位置。 UIScrollview有一些关于何时拖动开始和结束的通知,但没有任何内容可以不断更新其contentoffset。说我在X位置放置一个点。然后我向下滚动50个像素,该点需要平滑地向上移动50个像素(因此该点始终保持在相对于图像的相同位置)。

有没有人有任何想法?

编辑:添加代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    //other code...
    scrollView.delegate = self;
    scrollView.clipsToBounds = YES;
    scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
    scrollView.minimumZoomScale = (scrollView.frame.size.width  / imageView.frame.size.width);
    scrollView.maximumZoomScale = 10.0;    
    //other code...
}

添加一个点

-(void) foundTap:(UITapGestureRecognizer *) recognizer
{
    CGPoint pixelPos = [recognizer locationInView:self.view];

    UIImageView *testPoint = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"inner-circle.png"]];
    testPoint.frame = CGRectMake(0, 0, 20, 20);
    testPoint.center = CGPointMake(pixelPos.x, pixelPos.y);
    [self.scrollView addSubview:testPoint];

    NSMutableArray *tempTestPointArray = [[NSMutableArray alloc] initWithArray:testPointArray];
    [tempTestPointArray addObject:testPoint];
    testPointArray = tempTestPointArray;

    NSLog(@"recorded point %f,%f",pixelPos.x,pixelPos.y);
}

获取像素点

-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    NSLog(@"zoom factor: %f", scrollView.zoomScale);
    UIPinchGestureRecognizer *test = scrollView.pinchGestureRecognizer;

    UIView *piece = test.view;
    CGPoint locationInView = [test locationInView:piece];
    CGPoint locationInSuperview = [test locationInView:piece.superview];

    NSLog(@"locationInView: %@ locationInSuperView: %@", NSStringFromCGPoint(locationInView), NSStringFromCGPoint(locationInSuperview));
}

3 个答案:

答案 0 :(得分:2)

你能不能简单地用点作为滚动视图的子视图添加图像视图?

答案 1 :(得分:2)

您可以实施UIScrollViewDelegate方法scrollViewDidScroll:,以便在内容偏移发生变化时获得连续回调。

答案 2 :(得分:0)

您可以使用以下内容:

UIGraphicsBeginImageContext(mainimg.frame.size);
[mainimg.image drawInRect:
         CGRectMake(0, 0, mainimg.frame.size.width, mainimg.frame.size.height)];

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineCap(ctx, kCGLineCapButt);
CGContextSetLineWidth(ctx,2.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
if (largecursor == FALSE) {
    CGContextMoveToPoint(ctx, pt.x-3.0, pt.y);
    CGContextAddLineToPoint(ctx, pt.x+3.0,pt.y);
    CGContextMoveToPoint(ctx, pt.x, pt.y-3.0);
    CGContextAddLineToPoint(ctx, pt.x, pt.y+3.0);
} else {
    CGContextMoveToPoint(ctx, 0.0, pt.y);
    CGContextAddLineToPoint(ctx, maxx,pt.y);
    CGContextMoveToPoint(ctx, pt.x, 0.0);
    CGContextAddLineToPoint(ctx, pt.x, maxy);
}

CGContextStrokePath(ctx);
mainimg.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();