如何使可展开/可折叠ImageView
我有很多textViews
顶部scrollView
和每个textView
有一个自定义按钮,以便当用户单击该textView
它应该展开,当它点击回来时,它应该折叠到前一个位置。如何使可展开/可折叠ImageView
我在想什么做的是隐藏smallTextView
并显示expandedTextView
当按下按钮并按下按钮时我想隐藏expandedtextView
ñ显示smallTextView
。但我不知道该怎么做。任何帮助将不胜感激。
这里是我的代码:
-
(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = @"Demo";
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
for (int i=0;i<[appDelegate.serverResponseArray count];i++)
{
self.expandTextView = [[UITextView alloc] init];
[self.expandTextView setFrame:CGRectMake(8.0f, i*50.0f+10.0f, 270.0f, 40.0f)];
[self.expandTextView setBackgroundColor:[UIColor grayColor]];
[self.expandTextView setFont:[UIFont fontWithName:@"helvetica" size:12]];
[self.expandTextView setText:@"Welcome!!!"];
[self.expandTextView setTextColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1]];
[self.expandTextView setUserInteractionEnabled:NO];
[self.scrollView addSubview:self.expandTextView];
self.expandTextView = nil;
self.expandButton = [[UIButton alloc]initWithFrame:CGRectMake(8.0f, i*50.0f+1.0f, 270.0f, 60.0f)];
[self.expandButton setBackgroundColor:[UIColor clearColor]];
[self.expandButton addTarget:self action:@selector(textButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.expandButton.tag = i;
[self.scrollView addSubview:self.expandButton];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(278.0f, i*50.0f+10.0f, 14.0f, 40.0f)];
[imageView setBackgroundColor:[UIColor grayColor]];
[imageView setImage:[UIImage imageNamed:@"arrow.png"]];
[self.scrollView addSubview:imageView];
imageView = nil;
}
float maxHeight = 0;
for(UIView *v in [self.scrollView subviews])
{
if(v.frame.origin.x + v.frame.size.height > maxHeight)
maxHeight = v.frame.origin.x + v.frame.size.height;
}
self.scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, maxHeight+2570);
}
-(IBAction)textButtonClicked:(id)sender
{
NSLog(@"%@",sender);
}
和我怎么知道是越来越按下哪个按钮。
要知道你必须标记值分配给您的按钮,然后
-(IBAction)textButtonClicked:(id)sender
{
UIButton *btn=(UIButton*)sender;
NSLog(@"Tag of button = %d",btn.tag);
}
哪个按钮按我建议你改变你现有的ImageView的框架,而不是使用其他图像视图(隐藏和显示)
我编辑了我的问题,这些是textView,并且我已经为'viewDidLoad'中的按钮分配了标签值。如果你看到这行'self.expandButton.tag = i;'。即使改变框架尺寸也适用于我。你能指导更多的事情吗? – Zac24
首先,我建议您使用UITableView
而不是UIImageView
,然后实现Expandable和Collapsible UITableViewCell
。
有很多教程来实现此功能。只需谷歌它,你会有很多的解决方案。
例如:Expandable/Collapsible Table For iOS
检查由苹果公司提供的这个示例代码:Table View Animations and Gestures。
最好的方法是把UITableView放到UITableView的Cell上,当你点击它时改变单元的大小,我给你简单的例子来说明如何创建可展开的UITableViewCell。
http://www.roostersoftstudios.com/2011/04/14/iphone-uitableview-with-animated-expanding-cells/
它应该是表视图,而不是图像视图。 – Bhavin
imageview应该是一个tableview或uiview ... – IronManGill
这些不是表查看因为我不知道如何使tableView可扩展,这些都不是imageView要么。这些是textViews。 – Zac24