gpt4 book ai didi

ios - 如何从 NSArray 中选择一个 UIImgeView

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:51:08 26 4
gpt4 key购买 nike

我有用作可拖动对象的 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

主游戏VC.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

最佳答案

在您的头文件中声明一个属性来保存您的 ImageView ,然后将它们连接到界面构建器中。

@interface YourViewController : UIViewController

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

@end

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

enter image description here enter image description here

现在您需要使用 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
}
}

关于ios - 如何从 NSArray 中选择一个 UIImgeView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27636664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com