2012-01-17 54 views
-1

我想将多个图像放在一个xib中并单独移动它们。在xcode中移动多个图像

但是,当我点击其中一个图像时,另一个图像消失。我不想那样。

任何

+0

很可能需要添加更多的细节来描述你”重新做你想做的事。 – ThomasW 2012-01-17 00:42:36

回答

0

在您的视图控制器

#import "ImageMove.h" 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    ImageMove* imageMove = [[ImageMove alloc] initWithImage:[UIImage imageNamed:@"11.jpg"]]; 

[imageMove setFrame:CGRectMake(110, 60, [imageMove frame].size.width,[imageMove frame].size.height)]; 

[[self view] addSubview:imageMove]; 

[imageMove release]; 

ImageMove* imageMove1 = [[ImageMove alloc] initWithImage:[UIImage imageNamed:@"feder.jpg"]]; 

[imageMove1 setFrame:CGRectMake(110, 200, [imageMove1 frame].size.width,[imageMove1 frame].size.height)]; 

[[self view] addSubview:imageMove1]; 

[imageMove1 release]; 

}

这是你的ImageView类...

@interface ImageMove : UIImageView { 

} 

@end 

@implementation ImageMove 


    - (id) initWithImage:(UIImage *)image 
    { 
if (self = [super initWithImage:image]) 
    { 
    [self setUserInteractionEnabled:YES]; 

    [self setMultipleTouchEnabled:YES]; 
    } 

    return self; 

    } 


- (void)dealloc 
{ 

    [super dealloc]; 
    } 

    - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    { 

if ([touches count] == 1) 
{ 

    CGPoint newTouch = [[touches anyObject] locationInView:[self superview]]; 

    CGPoint lastTouch = [[touches anyObject] previousLocationInView: [self superview]]; 

    float xDif = newTouch.x - lastTouch.x; 

    float yDif = newTouch.y - lastTouch.y; 

    CGAffineTransform translate = CGAffineTransformMakeTranslation(xDif, yDif); 

    [self setTransform: CGAffineTransformConcat([self transform], translate)]; 

    } 


} 

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 


[self touchesEnded:touches withEvent:event]; 

} 




@end