0

我有一个带有图像的滚动视图。用户可以触摸滚动视图并在其上放置矩形视图。我希望当用户捏缩放滚动视图时,子子视图应根据比例自动调整大小。用户还应该能够通过触摸事件移动或调整子视图的大小。当滚动视图未缩放时,这工作正常,但当滚动视图缩放时,子视图不能在整个滚动视图移动时调整大小或拖动。我试图根据滚动视图大小更改子视图的框架大小,但没有给出正确的结果。

缩放滚动视图时有什么方法可以接收触摸事件

提前致谢

4

1 回答 1

0
@interface UIZoomingViewController : UIViewController <UIScrollViewDelegate>
    @property (strong, nonatomic) UIView* substrate;
@end

@implementation UIZoomingViewController

-(id)init /*your init method*/
{
    if(self = [super init])
    {
        UIScrollView* scrollView = ((UIScrollView*)self.view); /*controller view - UIScrollView*/
        [scrollView setDelegate:self];
        /*set contentsize, min max zoom scale*/

        _substrate = [UIView alloc] initWithFrame:scrollView.bounds];
        [scrollView addSubview:_substrate];

        /*
            if you need zoom view, add to substrate
            [_substrate addSubview:...];
        */
    }
    return self;
}

#pragma mark - UIScrollView delegate implementation

-(UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView
{
    return _substrate;
}

@end

和小技巧,如果你需要在 UIScrollView 之后接收触摸,创建子类......

@interface UINewScrollView : UIScrollView
@end

@implementation UINewScrollView

-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
    return YES;
}

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

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

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

@end
于 2012-06-26T18:52:09.927 回答