简体   繁体   English

轻按时,iOS Animate表格视图单元会变为全屏

[英]iOS Animate table view cell into full screen when tapped

I would like to implement the same type of functionality as this application: https://itunes.apple.com/us/app/fancy-units/id850306139?mt=8 我想实现与此应用程序相同的功能类型: https : //itunes.apple.com/us/app/fancy-units/id850306139?mt=8

When a user selects a table view cell, the cell animates to fill the entire screen and reveals a new view while animating. 当用户选择一个表格视图单元格时,该单元格会进行动画处理以填充整个屏幕,并在进行动画处理时显示一个新视图。

I have been trying to find information on how to do this but can't quite seem to find any good resources. 我一直在寻找有关如何执行此操作的信息,但似乎找不到任何好的资源。

How can I accomplish this? 我该怎么做? Even just an idea of what to properly google search will be extremely helpful! 即使只是一个关于如何正确地进行Google搜索的想法也将非常有帮助!

I don't know how Fancy Units does it, but I was able to replicate their look with this example. 我不知道Fancy Units是如何做到的,但是我能够通过此示例复制它们的外观。 The initial view controller (TableController) is a UIViewController subclass with a table view that displays basic cells. 初始视图控制器(TableController)是一个UIViewController子类,带有一个显示基本单元格的表视图。 I have another controller in the storyboard, ViewController that has a single table view cell (not inside a table view) pinned to the top of its view (constraints to the sides and top, and a height constraint of 44). 我在情节提要中还有另一个控制器ViewController,该控制器具有固定到其视图顶部的单个表格视图单元格(不在表格视图内部)(限制在侧面和顶部,高度限制为44)。 I add this controller as a child, and add it's view over top the cell you click on, and give it the cell's frame, background color, and text in the text label, then animate it to the full screen. 我将这个控制器添加为一个子控件,然后将其添加到您单击的单元格上方的视图中,并在文本标签中为其添加单元格的框架,背景色和文本,然后将其设置为全屏动画。 I have a delegate protocol defined in ViewController that's called when you click on the collapse button in the cell. 我在ViewController中定义了一个委托协议,当您单击单元格中的折叠按钮时会调用该协议。 The protocol's single method, expandedCellWillCollapse, is implemented in TableController to animate the controller back down. 该协议的单个方法expandCellWillCollapse在TableController中实现,以使控制器向下动画。 Here's the code, 这是代码,

#import "TableController.h"
#import "ViewController.h"

@interface TableController ()
@property (strong,nonatomic) NSArray *theData;
@property (weak,nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic) ViewController *expander;
@property (nonatomic) CGRect chosenCellFrame;
@end

@implementation TableController 


- (void)viewDidLoad {
    [super viewDidLoad];
    self.theData = @[@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine",@"Black",@"Brown",@"Red",@"Orange",@"Yellow",@"Green",@"Blue"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.theData.count;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor colorWithHue:.1 + .07*indexPath.row saturation:1 brightness:1 alpha:1];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.theData[indexPath.row];
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (! self.expander) {
        self.expander = [self.storyboard instantiateViewControllerWithIdentifier:@"Expander"];
        self.expander.delegate = self;
    }
    [self addChildViewController:self.expander];
    self.expander.view.frame = [self.tableView rectForRowAtIndexPath:indexPath];
    self.expander.view.center = CGPointMake(self.expander.view.center.x, self.expander.view.center.y - self.tableView.contentOffset.y); // adjusts for the offset of the cell when you select it
    self.chosenCellFrame = self.expander.view.frame;
    self.expander.view.backgroundColor = [UIColor colorWithHue:.1 + .07*indexPath.row saturation:1 brightness:1 alpha:1];
    UILabel *label = (UILabel *)[self.expander.cell viewWithTag:1];
    label.text = self.theData[indexPath.row];
    [self.view addSubview:self.expander.view];
    [UIView animateWithDuration:.5 animations:^{
        self.expander.view.frame = self.tableView.frame;
        self.expander.collapseButton.alpha = 1;
    } completion:^(BOOL finished) {
        [self.expander didMoveToParentViewController:self];
    }];
}


-(void)expandedCellWillCollapse { // delegate method called from the collapse button in the expanded cell
    [self.expander willMoveToParentViewController:nil];
    [UIView animateWithDuration:.5 animations:^{
        self.expander.view.frame = self.chosenCellFrame;
        self.expander.collapseButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self.expander.view removeFromSuperview];
        [self.expander removeFromParentViewController];
    }];
}

This isn't intended to be a compete solution, but it should get you started. 这并不是要成为竞争解决方案,但它应该可以帮助您入门。 I've uploaded this project here (edited 12/04/14 to make it work for iOS 8), http://jmp.sh/VKeIJLW . 我已经在这里上传了此项目(为了使其适用于iOS 8,已将其编辑(2014年12月4日)), http: //jmp.sh/VKeIJLW。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 轻触单元格时,表格视图单元格会更改高度 - Table View Cells change Height when cell tapped 多次点击时,表格视图单元格文本与其他文本重叠 - Table view cell text overlaps others when tapped multiple times 在 swift 中点击按钮时将单元格添加到表格视图 - Adding cell to Table view when button tapped in swift 轻按另一个时在表格视图单元格中取消选择一个按钮 - Deselect a button in a table view cell when another is tapped iOS:只有一个表格单元格,但屏幕上充满了单元格边框 - iOS: only one table cell, but the screen is full of cell borders 仅在视图内点击时选择表格单元格 - Select table cell only if tapped inside a view 如何在点击按钮时全屏显示.sheet() 以便将 go 显示到 SwiftUI 中的下一个视图? - How to present .sheet() full screen when a button is tapped in order for it to go to the next view in SwiftUI? 轻按表格单元时应用崩溃 - App crashes when table cell is tapped 如何从收藏夹视图单元中对图像进行动画处理,以通过单击将其移动到全屏 - How to animate a image from a collection view cell to move to full screen by tapping on it 当我们在Ios中点击它时,如何使用图像进行集合视图单元格“放大”和“缩小” - How to do collection view cell “zoom in” and “zoom out” with image when we tapped on it in Ios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM