0

在我的应用程序中有一个 UIView,其中一个 UIScrollView 中有一些带有图像(表单服务器)的 UIImageViews 在那里并动态加载。我的要求是当我触摸滚动视图图像中的图像时,必须在 UIView 中生成恰好位于 UIScrollView 图像顶部的图像。为此,我正在使用 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;
         }  
    }
}

提前致谢。

4

1 回答 1

0

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

于 2012-04-26T10:04:02.960 回答