繁体   English   中英

触摸UIScrollView中的图像时在UIView中生成图像

[英]Generating images in UIView when touches the image in UIScrollView

在我的应用程序中有一个UIView,其中有一个带有图像(窗体服务器)的UIImageViews并动态加载。 我的要求是,当我触摸滚动视图图像中的图像时,必须在UIScrollView图像顶部的UIView中生成该图像。 为此,我正在使用UITapGestureRecognizer。 图像在UIView中生成,但不完全在scrollView中图像的顶部。 任何人都可以帮忙提出建议。

鉴于负载是否有一个静态数组用于生成图像的坐标,我想动态计算

- (void)viewDidLoad
{
    [super viewDidLoad];
    xCordArray = [[NSArray  alloc]initWithObjects:@"30",@"78",@"126",@"174",  @"222",@"270",@"318",@"366",@"34",@"82",@"130",@"179",@"226",@"274",@"322",@"369",@"38" ,@"88",@"136", @"183",@"248",@"278",@"316",@"375", nil];
   UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]     initWithTarget:self action:@selector(singleTapGestureCaptured:)];
   [scrollView addGestureRecognizer:singleTap]; 
   [self populateScrollView];  
   scrollView.delegate = self;
   NSMutableArray *getEffectsImageData = [ud objectForKey:@"getimageeffects"];
   int scrollViewWidth = [getEffectsImageData count]*48;
   [scrollView setContentSize:CGSizeMake(scrollViewWidth, 40)];  
}

在下面的singleTapGestureCaptured方法中,将生成具有静态xCordArray值的图像。 我想动态生成。

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{ 
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSMutableArray *getEffectsImageData = [ud objectForKey:@"getimageeffects"];
    TonifyAppDelegate *appDelegate = (TonifyAppDelegate *)[UIApplication         sharedApplication].delegate;
    ImageToDrag *img = [[ImageToDrag alloc] init];
    for (int i=0; i<[getEffectsImageData count]; i++)
    {
         UIImageView *ImageView = [imageViewsArray objectAtIndex:i];
         NSString *sfxUrlFileName = [[getEffectsImageData objectAtIndex:i] lastPathComponent];
         NSLog(@"sfxUrlFileName: %@", sfxUrlFileName);
         NSData *imageData = [appDelegate    readSongDataFromDocsDirectory:sfxUrlFileName];
         UIImage *image = [[UIImage alloc] initWithData:imageData];
         CGPoint location = [gesture locationInView:self.scrollView];
         int xc = [[xCordArray objectAtIndex:i] intValue];// i want to calculate this xc dynamically.
         if(CGRectContainsPoint(ImageView.frame,location))
         {
             img = [img initWithImage:image];
             img.frame = CGRectMake(xc,240, 45, 42);
             img.userInteractionEnabled = YES;
             [self.view addSubview:img];
             img.tag = i;
         }  
    }
}

提前致谢。

我认为您必须替换img.frame = CGRectMake(xc,240, 45, 42); img.frame = [self.scrollView convertRect:ImageView.frame toView:self.view]; 它“有效”。 但是您的代码中还有很多奇怪的事情。 考虑将一个手势识别器添加到每个图像视图,而不是在滚动视图中添加一个“全局”手势识别器。 这样一来,您就可以摆脱循环,并且框架设置(这是您提出问题的原因)的复杂度也降低了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM