scrollview中的contentoffset [英] contentoffset in scrollview

查看:175
本文介绍了scrollview中的contentoffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

编辑:

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.

推荐答案

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

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

这篇关于scrollview中的contentoffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆