0

我正在尝试以编程方式创建多个图像,然后能够在视图中移动它们中的任何一个。我单击一个按钮,然后我可以移动该图像。我再次单击该按钮,我可以移动该图像,但不能再移动创建的第一张图像。我试图使用标签。

标题。

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];
}
4

3 回答 3

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

于 2013-01-28T04:47:55.600 回答
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];
}
于 2013-01-28T05:03:22.840 回答
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

已经测试和工作

祝你好运

于 2013-01-28T05:15:17.317 回答