我正在尝试以编程方式创建多个图像,然后能够围绕视图移动它们中的任何一个。我点击一个按钮然后我可以移动该图像。我再次单击该按钮,我可以移动该图像,但不再可以移动创建的第一个图像。我试图使用标签。
头。
UIImageView *imageView;
NSUInteger i;
和
@property (nonatomic, retain) UIImageView *imageView;
实施
@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];
}
答案 0 :(得分:1)
以下是我从您的代码中提出的内容
//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;
}
}
这就是移动图像的原因:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
//move the tapped image
self.selectedImageView.center = [touch locationInView:self.view];
}
记住重置图像计数器并在触摸结束时将selectedImageView设置为nil并触及取消阶段
答案 1 :(得分:0)
试试这个。
@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];
}
答案 2 :(得分:0)
此方法适用于手势识别器
@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
已经过测试和工作
祝你好运