如何在放大iphone sdk时调整滚动视图的子视图的大小

时间:2012-06-26 16:46:16

标签: iphone objective-c ios4 uiscrollview uitouch

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

滚动视图缩放时是否有任何方法可以接收触摸事件

提前致谢

1 个答案:

答案 0 :(得分: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