对滚动视图中选定的图像执行某些操作

发布于 2024-10-05 18:51:33 字数 1626 浏览 1 评论 0原文

我尝试制作以下内容:图像的水平列表,我可以选择它并对其执行某些操作。例如绕 x 轴翻转图像。我知道如何旋转图像。我创建了滚动视图并加载图像。当我点击图像时,我添加了事件处理程序。但我不知道如何处理点击图像。如何编写方法来对点击的图像执行某些操作?

- (void)viewDidLoad {
    [super viewDidLoad];        

    img1 = [UIImage imageNamed:@"imgTest.jpg"];
    img2 = [UIImage imageNamed:@"imgTest2.jpg"];

    arrayOfImages = [[NSMutableArray alloc] init];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];

    scrollView = [[UIScrollView alloc] init];
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = YES;
    scrollView.directionalLockEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor blueColor];
    scrollView.autoresizesSubviews = YES;
    scrollView.frame = CGRectMake(0, 0, 320, 128);
    [self.view addSubview:scrollView];

    UIImage *imageToAdd;
    int x = 0;
    int y = 0;
    for(imageToAdd in arrayOfImages)
    {
        UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];     

        temp.frame = CGRectMake(x, y, 128, 128);
        temp.userInteractionEnabled = YES;      
        x += 135;


        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
        [temp addGestureRecognizer:tap];    

        [scrollView addSubview:temp];
    }
...

- (void)imageTapped:(UIGestureRecognizer *)sender
{

    // how to get reference on selected item in scrollview???   
}

I try to make following: Horizontal list of images, that I can select and do something with it. For example flip image around x axis. I know how to rotate image. I created scrollview and load it with images. I added event handler when I tap on image. But I don't know how to do something with tapped image. How to code method to do something with tapped image?

- (void)viewDidLoad {
    [super viewDidLoad];        

    img1 = [UIImage imageNamed:@"imgTest.jpg"];
    img2 = [UIImage imageNamed:@"imgTest2.jpg"];

    arrayOfImages = [[NSMutableArray alloc] init];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];

    scrollView = [[UIScrollView alloc] init];
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = YES;
    scrollView.directionalLockEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor blueColor];
    scrollView.autoresizesSubviews = YES;
    scrollView.frame = CGRectMake(0, 0, 320, 128);
    [self.view addSubview:scrollView];

    UIImage *imageToAdd;
    int x = 0;
    int y = 0;
    for(imageToAdd in arrayOfImages)
    {
        UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];     

        temp.frame = CGRectMake(x, y, 128, 128);
        temp.userInteractionEnabled = YES;      
        x += 135;


        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
        [temp addGestureRecognizer:tap];    

        [scrollView addSubview:temp];
    }
...

- (void)imageTapped:(UIGestureRecognizer *)sender
{

    // how to get reference on selected item in scrollview???   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2024-10-12 18:51:33

手势识别器有一个 view 属性,该属性返回与识别器关联的视图。既然您知道它将是一个 UIImageView,您可以简单地转换它并使用它,如下所示:

UIImageView *iv = (UIImageView *)[sender view];

然后可以通过以下方式查询您的图像:

UIImage *image = [iv image];

如果您需要知道数组中的索引,有两种方法:要么简单地使用 < code>[arrayOfImages indexOfObject:image];,或者您可以为视图分配标签(数字)并使用它们。 Apple 不使用标签,它只是在这里,以便我们开发人员可以以某种方式“标记”视图。例如:

NSInteger counter = 0;
for(imageToAdd in arrayOfImages)
{
    UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];
    count.tag = counter++;
    ...
}

然后,在你的imageTapped:中,你可以通过tag查询索引:

NSInteger index = [imageView tag];

A gesture recognizer has a view property that returns the view associated with the recognizer. Since you know that it'll be a UIImageView you can simply cast it and use it as in:

UIImageView *iv = (UIImageView *)[sender view];

And your image can then be queried via:

UIImage *image = [iv image];

If you need to know the index in your array, there are two ways: either simply use [arrayOfImages indexOfObject:image];, or you can assign tags (numbers) to views and use them. A tag is not used by Apple, it's only here so we developers can "mark" views in some way. For example:

NSInteger counter = 0;
for(imageToAdd in arrayOfImages)
{
    UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];
    count.tag = counter++;
    ...
}

Then, in your imageTapped:, you can query the index via tag:

NSInteger index = [imageView tag];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文