1
votes

I have an UIScrollview and which contains UIView as a subview like,

scrollview with uiview

here i can able to drag the UIView inside the ScrollView, when the uiview goes to at END RIGHT or END LEFT the uiview disappears inside scrollview, after that i has to scroll the scrollview then i can able to see the uiview, here i need to SCROLL THE SCROLLVIEW when uiview comes at the rightside or left side, for that i used this code but not working.

[myscrollview scrollRectToVisible:myview.frame animated:YES];
1
tell me where do you use the above code? I mean in which method you put the line of code? - The iOSDev
In drag method that is dragging the myview using the pangesture - Harish
try to use one property of scroll view contentoffset this will provide you the proper coordinates in scroll view to move ie just modify the myview.frame.origin.x and .y by adding the contentoffset.x and .y respectively. - The iOSDev
will give it to you in while just wait for some time :) - The iOSDev
ok will give you some other code after testing it this time it will work for sure :) - The iOSDev

1 Answers

1
votes

Here I have used on UIButton (as it is already used in one of my application I dont need to write whole code again)

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

The cropRectangleButton is one UIButton and the imageMoved:withEvent: method is as below

- (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;
}

Here I change my contentSize of scrollView as it is very narrow to test that the scrollView is scrolling or not you don't need that line of code.

And when I drag the button left the scrollView is also automatically scrolled to the button frame to show the whole button try to implement this using UIView and if not possible just put one transparant UIButton on your view fully covered the view and you it to do the drag :)