簡體   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