我有一些代码允许我放大然后缩小到正常大小的图片但是如果我点击图片太多次它会创建该图片的大版本或之前视图中的图片无法调整大小。我不确定,但我认为这两段代码的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)];
问题还在于为什么图像最初需要两次触摸才会响应,然后它会在每次触摸时起作用,为什么我可以在靠近图像时触摸它仍然感觉到我正在触摸实际图像?这么多问题。
答案 0 :(得分:-1)
图像周围有触摸因为可能图像有黑色边框。你检查过了吗?