scrollview中的contentoffset

时间:2012-10-19 18:16:13

标签: iphone uiscrollview ios6 contentoffset

如何设置图像的内容偏移以追踪用户所在的图像并选择将其保存到相册。

 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);

编辑:

imageScrollView.contentOffset = CGPointMake(CGFloat x, CGFloat y);
在contentoffset中不确定放置什么,以便可以跟踪用户所在的图像并选择保存到相册

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

任何想法都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

您可以使用此代码(来自其中一个用于scrollview的Apple示例项目)来确定当前可见的“页面”在滚动视图中的位置。

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

一旦知道了这个索引号,就可以用它来确定要保存的图像