我想在一个xib中放置多个图像并单独移动它们。
然而,当我点击其中一个图像时,另一个图像消失了。我不希望这样。
任何
答案 0 :(得分: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