1

所以,我有这个UIScrollView在最初缩放时做得很好。我设置了最小和最大缩放比例并设置了必要的委托方法-(UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView;。最初我在上面设置了两个子视图(一个是一个小球),显然,一个会缩小而另一个没有(球视图)。我想改变它以便它们都正确缩放,所以我将较小的视图(球)添加到另一个子视图列表中。但是,现在没有缩放。如有必要,我可以发布代码,但我想知道这个问题是否对任何人都很熟悉,以及他们是否有答案。

编辑:这是代码

头文件

@interface NumberLineScroll : UIScrollView {

    NumberLine* nline;

    id <UIScrollViewDelegate> delegate;
}

@property(nonatomic, retain)id <UIScrollViewDelegate> delegate;


- (NumberLine*)getLine;

@end

主文件

#import "NumberLineScroll.h"

@implementation NumberLineScroll

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {

        [self setUserInteractionEnabled:YES];

        nline = [[NumberLine alloc] initWithFrame:CGRectMake(0, 0, 2100, 400)];
        [self addSubview:nline];

        self.minimumZoomScale = 0.5;
        self.maximumZoomScale = 3.0;
    }
    return self;
 }

- (NumberLine*)getLine {return nline;}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {}


- (void)dealloc {
    [nline release];
    [super dealloc];
}


@end

这是滚动视图。nline 只是一个带有球视图的视图和在drawRect. 这是委托类中的方法实现:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {}

- (void)doTest {NSLog(@"test");}

-(UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView {return [nline getLine];}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {

    NSLog(@"Zoom ended");
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    Ball* ball = [[nline getLine] getBall];
    CGPoint newCenter = ball.center;
    newCenter.y = ball.center.y * scrollView.zoomScale;
    newCenter.x = ball.center.x * scrollView.zoomScale;
    [[nline getLine] changeBalLoc:newCenter];
}
4

1 回答 1

1

我认为您想要在 UIScrollView 上进行缩放时管理子视图

请注意两件事,首先从下面的委托方法返回该 largeSubview(包含作为子视图的球视图)。

-(UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView
{
   return largeSubvew
  //here that view contains smallView(ball) too as mentioned you changed
}

在放大/缩小滚动视图时管理小视图的位置。

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
 CGPoint newCenter = smallerViewCenter;
 //smallerViewCenter contains the center point of smallerView(say ball),has set at load time, and only once
 newCenter.y = smallerView.y * scrollView.zoomScale;
 //new center is calculated on original position
 newCenter.x = smallerView.x * scrollView.zoomScale;
 smallerView.center = newCenter;

 }
于 2013-05-22T04:00:33.867 回答