我有一些代码可以让我放大然后缩小到正常大小的图片,但是如果我点击图片太多次,它会创建该图片的大版本或之前视图中的图片,它无法被调整大小。我不确定,但我认为这两条代码的 addSubview 语句之间可能存在矛盾或某些东西。这是ImageEnlarge
.
@implementation ImageEnlarge
-(UIImageView *)internal{
return internal;
}
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
internal = [[UIImageView alloc] initWithFrame:self.bounds];
[internal setBackgroundColor:[UIColor blackColor]];
[internal setContentMode:UIViewContentModeScaleToFill];
[self addSubview:(internal)];
}
return self;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)touch {
if (isLarge) [self makeSmall];
else [self makeFull];
}
-(void) makeFull {
[[self superview] bringSubviewToFront:self];
isLarge = YES;
CGSize largePicSize = CGSizeMake(156, 156);
CGPoint largePicOrigin = CGPointMake(110, 96);
CGRect largeFrame;
largeFrame.size = largePicSize;
largeFrame.origin = largePicOrigin;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[internal setFrame:self.bounds];
[self setFrame:largeFrame];
[UIView commitAnimations];
}
-(void) makeSmall {
isLarge = NO;
CGSize normPicSize = CGSizeMake(100, 100);
CGPoint normPicOrigin = CGPointMake(82, 74);
CGRect original;
original.size = normPicSize;
original.origin = normPicOrigin;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[internal setFrame:self.bounds];
[self setFrame:original];
[UIView commitAnimations];
}
@end
这是我的图像上的实现。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *albumArtImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.churchwebserver.org/imagename.jpg"]];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *albumArt = [UIImage imageWithData:albumArtImageData];
CGSize picSize = CGSizeMake(100, 100);
CGPoint picOrigin = CGPointMake(110, 96);
CGRect picFrame;
picFrame.size = picSize;
picFrame.origin = picOrigin;
ImageEnlarge * imEn =[[ImageEnlarge alloc]initWithFrame:picFrame];
[[imEn internal]setImage:albumArt];
[self.view addSubview:(imEn)];
问题还在于为什么图像最初需要两次触摸才能响应,然后每次触摸都会起作用,为什么我可以在靠近图像的地方触摸并且仍然感觉到我正在触摸实际图像?这么多的问题。