如何在 ios 中的弹出块上添加 UIImageView
How to add UIImageView on popup block in ios
嗨,我是 iOS 的新手,在我的项目中,我创建了一个 UITableView
图像,就像我下面的屏幕一样,没关系
在这里,当我点击 tableView 图像时,我想使用一个 UIView
.
显示带有弹出块的相关行图像
但是使用我的代码图像没有显示在弹出窗口中 UIView
。请看我下面的屏幕图像没有添加到 UIView
弹出块中,请帮我一些。
我的代码:
#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;
}
}
看起来是因为
popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
您创建视图并将其缩放到非常小,所有子视图也将缩小。然后在显示动画块中更改弹出框,但必须更改比例,如:
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?
您目前的实现有几个问题,包括:
- 您的体系结构不允许您获取所选单元格的索引路径 - 无论如何这将是一个问题,因此您必须制作这一部分。
用语言解释你必须改变的一切太难了,所以我为你写代码)我几乎不改变你的代码,但如果你把它改成我写的方式会很酷,因为apple guides比较方便
我做什么:
1) 为单元格创建自定义 class
2) 通过委托模式添加 cell say something to tableview 的能力
3) 当用户单击单元格中的按钮时 - 单元格告诉 tableview,哪个按钮被按下
4) table 视图将单元格图像的框架转换为自己的坐标并显示弹出窗口
你可以用这个替换你的代码:
#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 的新手,在我的项目中,我创建了一个 UITableView
图像,就像我下面的屏幕一样,没关系
在这里,当我点击 tableView 图像时,我想使用一个 UIView
.
但是使用我的代码图像没有显示在弹出窗口中 UIView
。请看我下面的屏幕图像没有添加到 UIView
弹出块中,请帮我一些。
我的代码:
#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;
}
}
看起来是因为
popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
您创建视图并将其缩放到非常小,所有子视图也将缩小。然后在显示动画块中更改弹出框,但必须更改比例,如:
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?
您目前的实现有几个问题,包括:
- 您的体系结构不允许您获取所选单元格的索引路径 - 无论如何这将是一个问题,因此您必须制作这一部分。
用语言解释你必须改变的一切太难了,所以我为你写代码)我几乎不改变你的代码,但如果你把它改成我写的方式会很酷,因为apple guides比较方便
我做什么: 1) 为单元格创建自定义 class 2) 通过委托模式添加 cell say something to tableview 的能力 3) 当用户单击单元格中的按钮时 - 单元格告诉 tableview,哪个按钮被按下 4) table 视图将单元格图像的框架转换为自己的坐标并显示弹出窗口
你可以用这个替换你的代码:
#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