簡體   English   中英

iOS sdk問題:如何將UIView轉換為UIImage視圖(另外我如何從GestureRecognzer獲取UIImageView?)

[英]iOS sdk question: how do I cast a UIView to a UIImage View (alternativly how do I get a UIImageView from a GestureRecognzer?)

期望的最終結果:用戶點擊UIImageView並將圖像更改為另一個圖像(后續點擊將圖像返回到原始狀態)

問題:我將一個(唯一的)選擇器添加到一堆UIImageViews(在一個數組中)並將動作指向同一個函數 - 讓我們調用這個函數imageTapped:for now。 到目前為止,這是我的代碼:

-(void)imageTapped:(UITapGestureRecognizer *)tapGesture {
    UIImageView *view = tapGesture.view;
    // rest of code...
}

這段代碼實際上運行正常,但是當我運行它時會收到警告:“不兼容的目標c類型初始化'struct UIView *',期望'struct UIImageView *'

有什么辦法擺脫這個? 不確定如何在目標c ...原始類型的工作正常工作,如(int)someFloat工作正常,但(UIImageView)someUiView不起作用。 就像我說的那樣,代碼在我運行它時可以正常工作,但是想要獲得編譯器警告。 任何幫助都會很棒....我對目標c(或任何非java語言)非常新,所以請保持溫和。 提前致謝。

您的本地變量“view”是指向UIImageView的指針,因此將其轉換為:

UIImageView *view = (UIImageView *)tapGesture.view;

不要忘記演員陣容中的星號。

對於任何UIController到您的視圖,其中任何鑄造Yourview是要投的觀點:

Yourview *yourView=(castYourview *)uivontroller.view
    - (void)viewDidLoad
{
self.view.backgroundColor = [UIColor orangeColor];
[self.view setIsAccessibilityElement:YES];
[scrollView1 setBackgroundColor:[UIColor blackColor]];
scrollView1.userInteractionEnabled = YES;
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES;        // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil];
onlyPngs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"]];
arrPngs = [onlyPngs mutableCopy];
// load all the images from our bundle and add them to the scroll view
kNumImages = [onlyPngs count];
NSInteger index;
for (index = 0; index <kNumImages; index++)
{
    NSString *imageName = [NSString stringWithString:[onlyPngs objectAtIndex:index]];
    if ([[imageName substringToIndex:1] isEqualToString: [[imageName substringToIndex:1] capitalizedString]]){
     UIImage *image = [UIImage imageNamed:imageName];
     UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
     // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
     CGRect rect = imageView.frame;
     rect.size.height = kScrollObjHeight;
     rect.size.width = kScrollObjWidth;
     imageView.frame = rect;
     imageView.userInteractionEnabled = YES;
     imageView.tag = index;// tag our images for later use when we place them in serial fashion
     imageView.accessibilityLabel = @"help";
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
     [imageView addGestureRecognizer:tap];
     [tap release];
     [scrollView1 addSubview:imageView];
     [imageView release];
    }    
}
[self layoutScrollImages];
 }
-(void)imageTapped:(UITapGestureRecognizer *)tapGesture{
UIImageView *view = (UIImageView *)tapGesture.view;
NSInteger  i=view.tag;
id iname =  [[self arrPngs] objectAtIndex:i];
NSLog(@"tag = %i %@",i,iname);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM