[英]UIButton in a custom UIView is not being triggered when tapped upon
[英]Close UIView when UIButton is tapped
在我的應用程序中,我有一個水平圖像庫,當我點擊圖像時, UIView
稱為顯示放大圖像。 UIView
包含一個scrollView
,以編程方式創建的imageviews
(動態創建)和一個UIButton
(關閉按鈕)。 但是,當我點擊關閉按鈕時,不會關閉UIView
。 在我的viewDidLoad中,我設置了gestureRecognizer
。
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)]];
這是我的singleTapAction方法:
- (void)singleTapAction:(UIGestureRecognizer *)singleTap_
{
MTGalleryPopUp* galleryImage = [[[NSBundle mainBundle] loadNibNamed:@"MTGalleryPopUp" owner:self options:nil] lastObject];
if(galleryImage.hidden)
{
NSLog(@"HIDDEN");
[galleryImage dismissWithAnimation:YES];
}
else
{
NSLog(@"NOT HIDDEN");
[self showGalleryDialog];
}
}
這是我的showGalleryDialog方法:
- (void)showGalleryDialog
{
MTGalleryPopUp * galleryView = [[[NSBundle mainBundle] loadNibNamed:@"MTGalleryPopUp" owner:self options:nil] lastObject];
[galleryView setReferenceVC:self];
[galleryView initWithTitle:@"Image Gallery"];
[galleryView setAlpha:0.0f];
[self.view addSubview:galleryView];
[UIView beginAnimations:nil context:nil];
[galleryView setAlpha:1.0];
[UIView commitAnimations];
}
這是我創建UIView
:
- (id)initWithTitle:(NSString *)aTitle
{
CGRect rect = [[UIScreen mainScreen] bounds]; // portrait bounds
if (self = [super initWithFrame:rect])
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
[self setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.8f]];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, TITLE_HEIGHT, screenRect.size.width, 350)];
[self.scrollView setUserInteractionEnabled:YES];
CloseButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[CloseButton addTarget:self
action:@selector(dismissWithAnimation:)
forControlEvents:UIControlEventTouchUpInside];
[CloseButton setTitle:@"X" forState:UIControlStateNormal];
[CloseButton.layer setBorderWidth:2];
[CloseButton.layer setBorderColor:[UIColor whiteColor].CGColor];
CloseButton.layer.cornerRadius=10;
CloseButton.layer.masksToBounds = YES;
[CloseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
CloseButton.titleLabel.font=[UIFont systemFontOfSize:16];
CloseButton.frame=CGRectMake(self.frame.size.width-80, 8, 20, 20);
[self addSubview:CloseButton];
strURL = [NSString stringWithFormat:@"%@%@?temp_url_sig=%@&temp_url_expires=%lld", URLstorage, CONTAINER_PATH, HMACStr, [@(floor([oneMinLater timeIntervalSince1970])) longLongValue]];
[mutableURLstorage addObject:strURL];
[imagesArray addObject:[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]]];
}
CGSize pageSize = CGSizeMake(ITEM_WIDTH, self.scrollView.frame.size.height);
_pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, screenRect.size.height-30, 320, 36)];
_pgCtr.backgroundColor=[UIColor grayColor];
int numberofPage=ceil((float)[imagesArray count]/2.5);
_pgCtr.numberOfPages= numberofPage;
self.scrollView.contentSize = CGSizeMake(450*numberofPage, pageSize.height);
int imgCurrentX = 10;
for (UIImageView* image in imagesArray)
{
@autoreleasepool
{
imageview = [[UIImageView alloc] initWithFrame:CGRectMake(imgCurrentX, 45, 200, self.scrollView.frame.size.height - 10)];
imageview.image = (UIImage*)image;
[imageview setUserInteractionEnabled:YES];
[self.scrollView addSubview:imageview];
imgCurrentX = imgCurrentX + 220;
}
}
[self addSubview:self.scrollView];
}
return self;
}
當我在調試,發生的事情是它可以追溯到singleTap_方法,並再次重新創建UIView
(現在二人UIViews
重疊)。 我在這里做錯了什么?
任何幫助將不勝感激。 謝謝。
我看到了幾個問題:MTGalleryPopUp未保存; 視圖未正確初始化; 我認為您可以通過更改“ alpha”來消除視圖,但是在singleTapAction中檢查“ hidden”屬性:-這是不同的事情。
將屬性galleryPopUp添加到根控制器:
@property MTGalleryPopUp *galleryPopUp;
這將是一個懶惰的初始化對象,添加以下方法:
- (MTGalleryPopUp *)galleryPopUp {
if (!_galleryPopUp) {
_galleryPopUp = [[[NSBundle mainBundle] loadNibNamed:@"MTGalleryPopUp" owner:self options:nil] lastObject];
}
return _galleryPopUp;
}
更改singleTapAction:
- (void)singleTapAction:(UIGestureRecognizer *)singleTap_
{
if(self.galleryPopUp.alpa == 0)
{
NSLog(@"HIDDEN");
[self showGalleryDialog];
}
else
{
NSLog(@"NOT HIDDEN");
[galleryImage dismissWithAnimation:YES];
}
}
並且showGalleryDialog中存在一個問題:
[galleryView initWithTitle:@"Image Gallery"];
應該在UIView中調用它,不是嗎?
您將創建“ MTGalleryPopUp”類的兩個對象。 因此,更好的方法是在“ .h”文件中聲明它,並在viewDidLoad中對其進行初始化。 根據每個水龍頭只是隱藏和顯示。 我希望它能解決您的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.