如何从 NSArray 中选择 UIImgeView [英] How to select an UIImgeView from NSArray

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

问题描述

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

I have UIImageViews which are used as drag-able objects and they are in NSArray so and they work fine when drag them but what i want is when i drag them and finish dragging method instead of drop the image in the UIImageView i want replace it with custom image only in the dragged finish. so my problem is if i remove the NSArray and make it only IF statement it work fine but only take one image and if i make it in NSArray as the code below it is take only last image (Close2) and not appended it

UIImage+Stuff.h

UIImage+Stuff.h

    #import <UIKit/UIKit.h>

    @interface UIImage (Stuff)

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

    @end

UIImage+Stuff.m

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

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

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

推荐答案

在你的头文件中声明一个属性来保存你的图像视图,然后将它们连接到界面构建器中.

In your header file declare a property to hold your imageview's and then hook them up in interface builder.

@interface YourViewController : UIViewController

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

@end

现在在界面构建器中,您需要将所有 imageView 连接到这个新属性.

Now in the interface builder you need to connect all the imageView's to this new property.

现在您需要使用 imageViews 集合.您甚至可以手动标记图像,以便稍后用于检查拖动的图像.

Now you need to work with the imageViews collection. You can manually even tag the image which can be later used to check which image is dragged.

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
  }
}

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

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