contentoffset in scrollview

2019-08-18 22:41发布

How can i set the contentoffset for image to track down which image user is on and selected to save it to phot album.

 UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

imageScrollView.pagingEnabled = YES;

NSInteger numberOfViews = 61;

for (int i = 0; i < numberOfViews; i++) {

CGFloat xOrigin = i * self.view.frame.size.width;

NSString *imageName = [NSString stringWithFormat:@"image%d.png", i];

_image = [UIImage imageNamed:imageName];

_imageView = [[UIImageView alloc] initWithImage:_image];

_imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);

UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
                                                   initWithTarget:self
                                                   action:@selector(handleLongPress:)];

imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];

[imageScrollView addSubview:_imageView];

imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);

Edit:

imageScrollView.contentOffset = CGPointMake(CGFloat x, CGFloat y);

in the contentoffset not sure what to put so that image can be tracked the user is on and selected to save to photo album

- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Save Photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
[actionSheet release];

 }}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

switch (buttonIndex) {
case 0:
    [self savePhoto];

   break;

default:
    break;

}

 -(void)savePhoto{

CGPoint location = [gesture locationInView:_imageView];

if  (CGRectContainsPoint(_imageView.bounds, location)){

UIImageWriteToSavedPhotosAlbum(_image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
  }}}

Any ideas will be appreciated.

1条回答
等我变得足够好
2楼-- · 2019-08-18 22:47

You can use this code (that comes from one of the apple sample projects for scrollview) to determine what the currently visible 'page' is in the scrollview.

// Calculate which page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

Once you know this index number you can use it to determine what image to save

查看更多
登录 后发表回答