Iphone 使用标记以编程方式创建多个图像后,在iOS中移动多个图像
我试图以编程方式创建多个图像,然后能够在视图中移动其中任何一个。我点击一个按钮,然后我可以移动该图像。我再次单击按钮,可以移动该图像,但不能再移动创建的第一个图像。我试着用标签Iphone 使用标记以编程方式创建多个图像后,在iOS中移动多个图像,iphone,ios,objective-c,uiimageview,Iphone,Ios,Objective C,Uiimageview,我试图以编程方式创建多个图像,然后能够在视图中移动其中任何一个。我点击一个按钮,然后我可以移动该图像。我再次单击按钮,可以移动该图像,但不能再移动创建的第一个图像。我试着用标签 UIImageView *imageView; NSUInteger i; 标题 UIImageView *imageView; NSUInteger i; 及 UIImageView *imageView; NSUInteger i; 实施 UIImageView *imageView;
UIImageView *imageView;
NSUInteger i;
标题
UIImageView *imageView;
NSUInteger i;
及
UIImageView *imageView;
NSUInteger i;
实施
UIImageView *imageView;
NSUInteger i;
@synthesize imageView;
-(IBAction)printTheImage:(id)sender{
UIImageView *theImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"animage.png"]];
self.imageView = theImage;
self.imageView.userInteractionEnabled = YES;
self.imageView.frame = CGRectMake(500, 500, 200, 200);
imageView.tag = i;
[self.view addSubview:self.imageView];
i++;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
CGPoint location = [touch locationInView:self.view];
if ([touch view]==self.imageView)
{
if (ImageView.tag == 1){
self.imageView.center = location;
}
if (imageView.tag == 2){
self.imageView.center = location;
}
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan:touches withEvent:event];
}
- (void)viewDidLoad{
i = 1;
[super viewDidLoad];
}
这是我从你的代码中得到的
UIImageView *imageView;
NSUInteger i;
//header
NSUInteger imageCount;
@property (nonatomic, retain) UIImageView *selectedImageView;
//implementation
@synthesize selectedImageView;
-(IBAction)printTheImage:(id)sender {
UIImageView *imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"animage"]];
[imageView sizeToFit];
imageView.frame = CGRectMake(500, 500, 200, 200);
imageView.userInteractionEnabled = YES;
imageView.tag = imageCount;
[self.view addSubview: imageView];
[imageView release];
imageCount++;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
//iterate trough all images
for (int i = 0; i < imageCount; i++) {
//get image from tag
UIImageView *imageView = (UIImageView *)[self.view viewWithTag: i];
//check which image was tapped
if (CGRectContainsPoint(imageView.frame, [touch locationInView:self.view])) {
NSLog(@"Image #%i was tapped",i);
self.selectedImageView = imageView;
//don't waste processor time for checking all other images, get the first and break the loop
break;
}
}
请记住在触摸结束和触摸取消阶段重置图像计数器并将selectedImageView设置为零。请尝试此操作
UIImageView *imageView;
NSUInteger i;
@property (nonatomic, copy) NSMutableArray *imageViews;
@synthesize imageView;
-(IBAction)printTheImage:(id)sender{
UIImageView *theImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"animage.png"]];
[self.imageViews adObject:theImage];
theImage.userInteractionEnabled = YES;
theImage.frame = CGRectMake(500, 500, 200, 200);
imageView.tag = [imageViews indexOfObject:theImage];
[self.view addSubview:theImage];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
CGPoint location = [touch locationInView:self.view];
NSInteger viewIndex = [self.imageViews indexOfObject:[touch view]];
if (viewIndex != NSNotFound)
UIView *theImage = self.imageViews[viewIndex];
theImage.center = location;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan:touches withEvent:event];
}
这种方法适用于手势识别器
UIImageView *imageView;
NSUInteger i;
@interface ViewController : UIViewController {
int tags;
}
- (IBAction)create:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//Tag to start
tags = 1;
}
- (IBAction)create:(id)sender
{
UIImageView *temp = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 200, 200)];
temp.image = [UIImage imageNamed:@"yourimage.jpg"];
temp.userInteractionEnabled = YES;
temp.tag = tags;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePan:)];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[temp addGestureRecognizer:pan];
[temp addGestureRecognizer:tap];
[self.view addSubview:temp];
tags++;
}
//This is just to check if the tags are working
-(void)handleTap:(UITapGestureRecognizer *)recognizer
{
UIView *piece = recognizer.view;
int index = piece.tag;
NSString *msg = [NSString stringWithFormat:@"Tag:%i", index];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test"
message:msg
delegate:self
cancelButtonTitle:@"ok"
otherButtonTitles:nil, nil];
[alert show];
}
//Drag action
-(void)handlePan:(UIPanGestureRecognizer *)recognizer
{
UIView *piece = recognizer.view;
if ([recognizer state] == UIGestureRecognizerStateBegan || [recognizer state] == UIGestureRecognizerStateChanged)
{
CGPoint translation = [recognizer translationInView:[piece superview]];
[piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
[recognizer setTranslation:CGPointZero inView:[piece superview]];
[self.view bringSubviewToFront:piece];
}
}
@end
已经测试和工作
UIImageView *imageView;
NSUInteger i;
好运无关:根据苹果的编码惯例,变量名应该以小写字母开头。您应该使用
imageView
而不是imageView
ImageView
表示一个类名,作为变量名使用会令人困惑。这里的问题是,在第二次函数调用时,您使用属性将UIImageView的上一个引用替换为保留它时,您需要以某种方式保存新创建对象的引用,如NSMutableArray,我的意思是,ImageView属性只能包含1个引用,这将是您每次调用printTheImage:函数时创建的新对象。
UIImageView *imageView;
NSUInteger i;