ScrollRectToVisible函数

时间:2012-11-15 08:55:34

标签: objective-c ios uiscrollview

我有一个UIScrollview,其中包含UIView作为子视图,

scrollview with uiview

这里我可以在UIView内拖动ScrollView,当uiview进入 END RIGHT END LEFT uiview在scrollview中消失,之后我必须滚动滚动视图然后我才能看到uiview,这里我需要滚动SCROLLVIEW 当uiview出现在右侧或左侧时,我使用这段代码但没有工作。

[myscrollview scrollRectToVisible:myview.frame animated:YES];

1 个答案:

答案 0 :(得分:1)

这里我用过UIButton(因为它已经在我的一个应用程序中使用过,我不需要再次编写整个代码)

[cropRectangleButton addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];

cropRectangleButton是一个UIButton,imageMoved:withEvent:方法如下

- (IBAction) imageMoved:(id)sender withEvent:(UIEvent *)event
{
    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];

    CGPoint prev = lastTouchDownPoint;
    lastTouchDownPoint = point;
    CGFloat diffX = point.x - prev.x;
    CGFloat diffY = point.y - prev.y;

    UIControl *button = sender;
    CGRect newFrame = button.frame;
    newFrame.origin.x += diffX;
    newFrame.origin.y += diffY;
    scrollView.contentSize = CGSizeMake(2*scrollView.frame.size.width, scrollView.frame.size.height);
    [scrollView scrollRectToVisible:newFrame animated:YES];
    button.frame = newFrame;
}

这里我更改了scrollView的contentSize,因为它非常窄,以测试scrollView是否滚动,你不需要那行代码。

当我向左拖动按钮时,scrollView也会自动滚动到按钮框以显示整个按钮尝试使用UIView实现此功能,如果不可能只是在视图上放置一个透明的UIButton完全覆盖视图而你做拖动:)