如何从 NSArray 中选择 UIImgeView

2024-03-10

I have UIImageView用作可拖动对象,它们位于NSArray所以,当拖动它们时它们工作得很好,但我想要的是当我拖动它们并完成拖动方法而不是将图像放在UIImageView我只想在拖动完成时将其替换为自定义图像。所以我的问题是如果我删除NSArray并使其仅 IF 语句工作正常,但只拍摄一张图像,如果我将其放入NSArray正如下面的代码所示,它仅获取最后一张图像(Close2),而不附加它

UIImage+Stuff.h

    #import <UIKit/UIKit.h>

    @interface UIImage (Stuff)

    //
    // return an UIImage from a CALayer
    //
    + ( UIImage* ) grabImage:(CALayer*)layer;

    @end

UIImage+Stuff.m

    #import "UIImage+Stuff.h"

    @implementation UIImage (Stuff)

    + ( UIImage* ) grabImage:(CALayer*)layer
    {
    UIGraphicsBeginImageContext ( layer.frame.size );
    [ layer renderInContext:UIGraphicsGetCurrentContext() ];

        UIImage *grab = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return grab;
    }
    @end

MainGameVC.h

    @class APChartObject;

    //
    // drag status
    //
    typedef enum {
    tDragStatusBegin = 0,
    tDragStatusEnd,
    tDragStatusIntersectIn,
    tDragStatusIntersectOut
    } tDragStatus;

    @interface MainGameVC : UIViewController

    @property (nonatomic, strong) IBOutletCollection(UIImageView) NSArray *TopMenuImages;
    //top menu scroll views
    @property (nonatomic, retain) IBOutlet UIScrollView *TopMenuViewer;
    @property (nonatomic, retain) IBOutlet UIScrollView *scrollview;

    //drag and drop
    @property (nonatomic, strong) IBOutlet UIView *DropView;
    @property (nonatomic, strong) UIImageView *dragObject;
    @property (nonatomic, strong) IBOutlet UIScrollView *cart;
    @property (nonatomic, assign) tDragStatus dragging;
    @property (nonatomic, strong) APChartObject *selectedModel;

    // all tools
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectBox;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectSand;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectSoil;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectWater;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectWheat;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectCorn;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectGarlic;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectLettuse;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectOnion;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectSugercane;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectTomato;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectCucumber;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectBeans;
    @end

MainGameVC.m

    #import "UIImage+Stuff.h"
@interface MainGameVC ()
{
    UIImageView *_selectedView;
    CGPoint _startPoint;
    int selectedViewTag;
}

@end

- (void)viewDidLoad
{
    // drag and drop touch
    UIPanGestureRecognizer *DragAndDrop = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
    [self.TopMenuViewer addGestureRecognizer:DragAndDrop];

    NSArray *imageViewArray = [NSArray arrayWithObjects:DragedObjectWheat,DragedObjectCorn,DragedObjectOnion, nil];

    for(UIImageView *image in imageViewArray)
    {
        UIPanGestureRecognizer *DragAndDrop2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:) ];
        [DragAndDrop2 setMinimumNumberOfTouches:1];

        [image addGestureRecognizer:DragAndDrop2];
        image.userInteractionEnabled = YES;

    }

}

//+---------------------------------------------------------------------------+
#pragma mark - View exchange
// +---------------------------------------------------------------------------+


    //
    // make the tricks.
    // Add a subview with the screenshot of selected and move around the screen
    //
    - ( void) cloneViewWithCenter:(CGPoint)point image:(UIImage*)grab ImageTag:(int)ToolTag
    {
    if ( _selectedView ) [ _selectedView removeFromSuperview ];
    selectedViewTag = ToolTag;
    _selectedView = [[ UIImageView alloc ] initWithImage:grab ];
    _selectedView.frame = CGRectMake(point.x, point.y, grab.size.width,   grab.size.height);
    _selectedView.userInteractionEnabled = YES;

    [ self.view addSubview:_selectedView ];

    UIPanGestureRecognizer *pan = [[ UIPanGestureRecognizer alloc ] initWithTarget:self  action:@selector(moveObject:) ];
    [ pan setMinimumNumberOfTouches:1 ];
    [ _selectedView addGestureRecognizer:pan ];
    }


    // +---------------------------------------------------------------------------+
    #pragma mark - Refresh
    // +---------------------------------------------------------------------------+


    //
    // refresh loop
    //
    - (void) refreshView
    {
    [UIView animateWithDuration:0.2 animations:^
     {
         CGRect r = _selectedView.frame;

         switch ( _dragging ) {
             case tDragStatusBegin:
                 r.size.width  *= 1;
                 r.size.height *= 1;
                 break;
             case tDragStatusEnd:
                 r.size.width  /= 1;
                 r.size.height /= 1;
                 break;
             case tDragStatusIntersectIn:
                 r.size.width  = 1;
                 r.size.height = 1;

                 [ self finishDrag ];
                 break;
             case tDragStatusIntersectOut:
                 _selectedView.center = _startPoint;
                 break;
         }

         _selectedView.frame = r;

     } completion:^(BOOL finished)
     {

         if ( _dragging == tDragStatusIntersectOut )
             _selectedView.hidden = YES;

     }];
    }

    //
    // end drag
    //
    - (void) finishDrag
    {
    UIImageView *Tool;

    UIImage *ReplacedPhoto;
    UIImageView *imageView;
    switch (selectedViewTag)
    {

        case 1:

        {
            logic for image tag 1
                break;
        }
        case 2:

        {
            logic for image tag 2
                break;
        }

            Tool.userInteractionEnabled = YES;
            [self appendView:Tool];
    }

    //
    // check for insertion in cart (or not)
    //
    - (void) checkForIntersection
    {
        //
        // ABS coords.
        //
        CGRect childRect = [ self.view convertRect:_selectedView.frame fromView:nil ];
        CGRect cartRect  = [ self.view convertRect:_cart.frame fromView:nil ];

        if ( CGRectIntersectsRect ( childRect, cartRect ))
        {
            self.dragging = tDragStatusIntersectIn;

        }
        else
        {
            self.dragging = tDragStatusIntersectOut;
        }
    }

    - (void) refreshCart
    {
        [ _cart setContentOffset:CGPointMake(_cart.contentOffset.x, 0) animated:YES ];
    }


    // +---------------------------------------------------------------------------+
    #pragma mark - Pan gesture
    // +---------------------------------------------------------------------------+

    - ( void ) panDetected:(UIPanGestureRecognizer*)gesture
    {

        CGPoint pInView = [ gesture locationInView:self.view ];
        //CGSize  pSize   = gesture.view.frame.size;

        if ( gesture.state == UIGestureRecognizerStateBegan )
        {
            _startPoint = pInView;
            UIImage *grab = [UIImage grabImage: gesture.view.layer];
            ToolTag = gesture.view.tag ;
            for (int i = 0; i>ToolTag; i++)
            {
                i = i+1;
                ToolTag = i;
            }
            //
            // centering view
            //
            //pInView.x = pInView.x - pSize.width/2;
            //pInView.y = pInView.y - pSize.height/2;

            [ self cloneViewWithCenter:pInView image:grab ImageTag:ToolTag ];

            self.dragging = tDragStatusBegin;
        }
        else if ( gesture.state == UIGestureRecognizerStateChanged )
        {
            [ self moveObject:gesture ];
        }
        else if ( gesture.state == UIGestureRecognizerStateEnded )
        {
            self.dragging = tDragStatusEnd;
            [ self checkForIntersection ];
        }
    }

    //
    // move draggable view around
    //
    - (void) moveObject:(UIPanGestureRecognizer *)pan
    {
        _selectedView.center = [ pan locationInView:_selectedView.superview ];
    }


// +---------------------------------------------------------------------------+
#pragma mark - Setter
// +---------------------------------------------------------------------------+


    - (void)setDragging:(tDragStatus)dragging
    {
        _dragging = dragging;
        [ self refreshView ];
    }


    // +---------------------------------------------------------------------------+
#pragma mark - Chart view
    // +---------------------------------------------------------------------------+


    //
    // recursively append view to scrollview.
    // If position already contains a view, shift and retry.
    //
    - (void) appendView:(id)view
    {

        [ _cart addSubview:view ];

        [ self performSelector:@selector(refreshCart) withObject:nil afterDelay:0 ];
    }
    @end

在您的头文件中声明一个属性来保存您的图像视图,然后将它们连接到界面生成器中。

@interface YourViewController : UIViewController

@property (nonatomic, strong) IBOutletCollection(UIImageView) NSArray *imageViews;
//...

@end

现在,在界面生成器中,您需要将所有 imageView 连接到这个新属性。

现在您需要与imageViews收藏。您甚至可以手动标记图像,以便稍后用于检查拖动了哪个图像。

for (UIImageView *imageView in self.imageViews) {
  if (imageView.tag == 1){ 
  // .. Logic for image with tag 1
  }
  elseif (imageView.tag == 2){ 
  // .. Logic for image with tag 2
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 NSArray 中选择 UIImgeView 的相关文章

随机推荐

  • 在 FastAPI 端点中进行并发.futures.ThreadPoolExecutor 调用是否危险?

    我有以下测试代码 import concurrent futures import urllib request URLS http www foxnews com http www cnn com http europe wsj com
  • 如何用CSS隐藏文本?

    有这个代码 h4 class ihf price style margin right 10px span class ihf for sale price 16 750 000 span Fee Simple h4 如何隐藏文本 费用简单
  • ggplot2 2D 密度图 - 渐变填充太平滑

    I am having some difficulty with the ggplot2 package and the gradient fill For my data with low number of data points it
  • 如何在 python(或 numpy/scipy)中生成复杂的高斯白噪声信号?

    我正在做一些关于DSP 数字信号处理 的工作 需要生成离散的复杂高斯白噪声信号 我知道我可以使用numpy random normal 0 1 n 生成离散序列 但它是在实数域中 用Matlab模拟很容易 但是我想知道如何用python替换
  • 设置php的时间限制

    我正在尝试在预订系统中设置时间限制 这样用户必须 可以删除其预订 但不能在输入预订后 1 分钟内删除
  • 检查 iOS 上是否安装了配置文件

    我正在尝试检查 iOS 设备上是否安装了 mobileconfig 文件 我尝试过此链接描述的方法http blog markhorgan com p 701 http blog markhorgan com p 701 但我失败了 也许有
  • 通过java列出weblogic中的所有用户

    有谁知道如何在java中列出所有weblogic用户 例如 安全领域有 5 个用户 我想获取所有用户 我该怎么做 这很容易 为了将来参考 如果您想查找诸如 我如何使用 weblogic 和 Java 做 X 之类的内容 请使用JMX在你的谷
  • IntelliJ IDEA 中的 Java 项目文件夹结构

    IntelliJ IDEA 中 Java 项目可接受的文件夹结构是什么 多个来源 像这样 https stackoverflow com a 28161314 4490400 建议采用以下结构 idea src main java com
  • svg:svg 是什么意思?

    这是什么意思 append svg svg 我在 HTML 和 D3 代码中看到了它 是否添加了SVG插件 在 XHTML 代码中 可以使用命名空间来区分网页中包含的其他基于 XML 的语言 这里 命名空间 svg 用在标签 svg 之前
  • 使用 SWIG 将 C 结构体数组访问到 Python

    我尝试从 Python 调用现有的 C 代码 C代码定义了一个结构体B包含一个结构体数组As C 代码还定义了一个函数 该函数在调用时将值放入结构中 我可以访问数组成员变量 但它不是列表 或支持索引的东西 相反 我得到的是一个代理对象B I
  • jQuery Closest() 不适合我(或者我不为它工作)

    鉴于这个 jQuery div MvcFieldWrapper input focus function this closest label MvcDynamicFieldError fadeOut 并给出这个 HTML div clas
  • R:箱线图 - 如何向下移动 x 轴标签?

    RGR Treatment Geno boxplot fit lt aov Total RGR Treatment Geno data For R summary fit t lt TukeyHSD fit t boxplot Total
  • 用razor显示编码的html

    我将编码的 HTML 存储在数据库中 我可以正确显示它的唯一方法是 div class content MvcHtmlString Create HttpUtility HtmlDecode Model Content div 它很丑 有没
  • 使用Python将海量数据批量插入SQLite

    我读到了这个 使用 Python 将 CSV 文件导入 sqlite3 数据库表 https stackoverflow com questions 2887878 importing a csv file into a sqlite3 d
  • 如何使用 ruby​​ 将图像文件转换为字节数组

    我需要将图像作为字节数组传递到 NET SOAP Web 服务 谁能举例说明如何使用 ruby 将上传的图像文件转换为字节数组 如果我正确理解您的问题 您将获得某种格式的图像 例如 jpeg 或 png 但您需要像素数组才能发送到 SOAP
  • 在带有选项卡的 Winforms 的模型视图演示器中应该使用多少个演示器?

    我有一个带有与业务实体相关的选项卡的表单 例如一个人有传记数据 地址数据等 每个选项卡处理一类个人数据的输入 编辑 并且每个选项卡可以独立保存 应该为所有选项卡使用一名演示者 还是每个选项卡使用一名演示者 还可能有一个主选项卡 它可以导航到
  • 出现 Axios 错误:拨打大量电话时连接 ETIMEDOUT

    从我的 Azure Function 进行大量调用时出现此错误 这个错误是什么意思 如何排除故障 我猜我的 TCP 套接字用完了 我真的不知道如何在功能应用程序菜单中检查它 不过 我检查了 Azure Maps API 的日志 没有错误或掉
  • git 中带有子模块的浅克隆,如何使用指向提交而不是最新提交? [复制]

    这个问题在这里已经有答案了 我知道可以使用浅层子模块 depth选项与git submodule update 但是 如果我跑 git submodule update init recursive depth 1 在我的带有多个子模块的项
  • 如何更改动态 SQL 中的序列?

    我正在尝试创建一个脚本来将数据从一个数据库迁移到另一个数据库 我当前无法做的一件事是将序列的 nextval 设置为另一个数据库中序列的 nextval 我从 user sequences 中得到了值的差异 并生成了以下动态 SQL 语句
  • 如何从 NSArray 中选择 UIImgeView

    I have UIImageView用作可拖动对象 它们位于NSArray所以 当拖动它们时它们工作得很好 但我想要的是当我拖动它们并完成拖动方法而不是将图像放在UIImageView我只想在拖动完成时将其替换为自定义图像 所以我的问题是如