gpt4 book ai didi

ios - 如何在 ios 中的弹出 block 上添加 UIImageView

转载 作者:行者123 更新时间:2023-11-29 01:22:47 25 4
gpt4 key购买 nike

嗨,我对 iOS 很陌生,在我的项目中,我创建了一个 UITableView ,其图像如下面的屏幕所示,没关系

在这里,当我点击 tableView 图像时,我想使用一个 UIView 显示带有弹出 block 的相关行图像。

但是使用我的代码图像没有显示在弹出的 UIView 上。请查看我的下面的屏幕图像未添加到 UIView 弹出 block 上,请帮助我。

我的代码:

#import "imageTableViewController.h"

@interface imageTableViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *mainTable;
NSMutableArray *imageArray;
UIButton *imageButton;
UIView *popUpView;
BOOL isFullScreen;
CGRect prevFrame;
}

@end

@implementation imageTableViewController

- (void)viewDidLoad {
[super viewDidLoad];

imageArray = [[NSMutableArray alloc] initWithObjects:@"flower.jpeg",@"Bird.jpg",@"Browser.jpeg", nil];
mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
mainTable.dataSource = self;
mainTable.delegate = self;
[self.view addSubview:mainTable];

popUpView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 25, 25)];
popUpView.backgroundColor = [UIColor orangeColor];
popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[mainTable addSubview:popUpView];

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
imageView.image = [UIImage imageNamed:@"flower.jpeg"];
[popUpView addSubview:imageView];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return imageArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil)
{
Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}
imageButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
[imageButton setImage:[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]] forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(imgToFullScreen:) forControlEvents:UIControlEventTouchUpInside];
[Cell.contentView addSubview:imageButton];


return Cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}

-(void)imgToFullScreen:(UITapGestureRecognizer*)sender {
if (!isFullScreen) {
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
popUpView.hidden = NO;
prevFrame = CGRectMake(30, 30, 25, 25);
[popUpView setFrame:CGRectMake(120, 50, 150, 150)];
}completion:^(BOOL finished){
isFullScreen = TRUE;
}];
return;
}
else{
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[popUpView setFrame:prevFrame];

}completion:^(BOOL finished){
isFullScreen = FALSE;
popUpView.hidden = YES;
}];
return;
}
}

enter image description here

最佳答案

看起来是因为

popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);

您创建 View 并将其缩放到非常小,所有 subview 也将缩小。然后在显示动画 block 中更改弹出窗口的框架,但您必须更改比例,例如:

popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);

编辑到期评论

hmm yes that's fine and one small problem is coming @in.disee that is when i zoom in popup block images are popup like my above screen and when i zoom out that images are must zoom out near related rows,understand?

您目前的实现几乎没有问题,包括:

  • 您的体系结构不允许您获取所选单元格的索引路径 - 无论如何这都会是一个问题,因此您必须制作这一部分。

用语言解释你必须改变的一切太难了,所以我为你写代码)我几乎不改变你的代码,但如果你把它改成我写的方式会很酷,因为它是苹果指南更方便

我做什么:1)为单元格创建自定义类2) 通过委托(delegate)模式为 cell say something to tableview 添加能力3)当用户点击单元格中的按钮时 - 单元格告诉tableview,按下了哪个按钮4) 表格 View 将单元格图像的框架转换为自己的坐标并显示弹出窗口

你可以用这个替换你的代码:

#import "imageTableViewController.h"


@class CustomCell;
@protocol CustomCellDelegate <NSObject>
- (void)didSelectImageNamed:(NSString *)name fromCell:(CustomCell *)cell;
@end

@interface CustomCell : UITableViewCell
@property (nonatomic, strong) NSString *imageName;
@property (nonatomic, strong) UIButton *imageBtn;
@property (nonatomic, weak) id <CustomCellDelegate> delegate;
@property (nonatomic, assign, readonly) CGRect imageRect;
@end

@implementation CustomCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self){
[self setup];
}
return self;
}

- (CGRect)imageRect {
return self.imageBtn.frame;
}

- (void)setup {
self.imageBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
[self.imageBtn addTarget:self action:@selector(imgToFullScreen:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.imageBtn];
}

- (void)prepareForReuse {
[self.imageBtn setImage:nil forState:UIControlStateNormal];
}

- (void)setImageName:(NSString *)imageName {
_imageName = imageName;
[self.imageBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}

- (void)imgToFullScreen:(UIButton *)sender {//btw it's not UITapGestureRecognizer *
[self.delegate didSelectImageNamed:self.imageName fromCell:self];
}

@end


@interface imageTableViewController ()
<CustomCellDelegate>
@end

@implementation imageTableViewController {
UITableView *mainTable;
NSMutableArray *imageArray;
UIButton *imageButton;
UIView *popUpView;
BOOL isFullScreen;

CGRect prevFrame;
}

- (void)viewDidLoad {
[super viewDidLoad];

imageArray = [[NSMutableArray alloc] initWithObjects:@"flower.jpeg",@"Bird.jpg",@"Browser.jpeg", nil];
mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
mainTable.dataSource = self;
mainTable.delegate = self;
[self.view addSubview:mainTable];

popUpView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 25, 25)];
popUpView.backgroundColor = [UIColor orangeColor];
popUpView.hidden = YES;
[mainTable addSubview:popUpView];

UIView * imageView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
imageView.backgroundColor = [UIColor colorWithRed:255./255. green:0 blue:0 alpha:1];
[popUpView addSubview:imageView];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return imageArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil)
{
Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Cell.delegate = self;
}
Cell.imageName = [imageArray objectAtIndex:indexPath.row];

return Cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}

-(void)imgToFullScreen:(NSString *)imageName fromRect:(CGRect)frame {

if (!isFullScreen) {

[popUpView setFrame:frame];
[popUpView setHidden:NO];

[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[popUpView setFrame:CGRectMake(120, 50, 150, 150)];
}completion:^(BOOL finished){
isFullScreen = YES;
prevFrame = frame;
}];
return;
}
else{
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[popUpView setFrame:prevFrame];
}completion:^(BOOL finished){
isFullScreen = NO;
popUpView.hidden = YES;
}];
return;
}
}


#pragma mark - CustomCellDelegate

- (void)didSelectImageNamed:(NSString *)name fromCell:(CustomCell *)cell {
CGRect imageFrame = cell.imageRect;
CGRect imageFrameGlobalCoord = [mainTable convertRect:imageFrame fromView:cell];

[self imgToFullScreen:name fromRect:imageFrameGlobalCoord];
}

@end

关于ios - 如何在 ios 中的弹出 block 上添加 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34395669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com