Custom UICollectionViewCell contentView animation - ios

I change the height of a UICollectionViewCell if a user touches it. To achieve that I use performBatchUpdates: to change the underlying data and use the standard cell animation. This works perfectly and the change gets animated with a standard grow and shrink animation.
Additionally I use reloadSection to update all subviews of the cells in that section. Here is my code:
[collectionView performBatchUpdates:^{
dataHelper.isCellExpanded = !dataHelper.isCellExpanded;
} completion:^(BOOL finished) {
[collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
}];
I need to achieve a flexible reordering of the subviews for the expanded cell state. So I don't use Auto Layout. I only change the subviews frame and would like to animate the frame change along with the UICollectionViewCell animation.
The Problem is that the subviews inside the contentView of the cells get updated without animation and after the height change animation of the cell is finished. But I would like to animate the cell subviews along with the animation of the height change. How would one implement that?
UPDATE
A custom animation method in my UICollectionViewCell looks like this:
- (void)animate {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^
{
CGRect labelFrame = self.testLabel.frame;
labelFrame.origin.y += 70;
self.testLabel.frame = labelFrame;
}
completion:nil];
}
I create the testLabel in the initWithFrame: of my UICollectionViewCell subclass like so:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
CGRect cellFrame = CGRectMake(0, 0, frame.size.width, frame.size.height);
// content
_testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, frame.size.height - 20, frame.size.width - 20, 20)];
_testLabel.backgroundColor = [UIColor redColor];
_testLabel.textColor = [UIColor blackColor];
_testLabel.text = #"test";
[self.contentView addSubview:_testLabel];
....

You have to implement an animation method inside the collection view cell and you need to that call from the performBatchUpdates method.
[collectionView performBatchUpdates:^{
dataHelper.isCellExpanded = !dataHelper.isCellExpanded;
// Access the cells you want to animate
yourCustomCell *cell = (yourCustomCell *)[collectionView cellForItemAtIndexPath:indexPath];
[cell animate];
} completion:^(BOOL finished) {
[collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
}];
Inside the cell animate method you can implement all the animations you might want.

I was able to solve this by just adding springs and struts to the contentView and to the testLabel in my UICollectionViewCell subclass in the form of autoresizingMask
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
CGRect cellFrame = CGRectMake(0, 0, frame.size.width, frame.size.height);
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// content
_testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, frame.size.height - 20, frame.size.width - 20, 20)];
_testLabel.backgroundColor = [UIColor redColor];
_testLabel.textColor = [UIColor blackColor];
_testLabel.text = #"You rock";
_testLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[self.contentView addSubview:_testLabel];
Side note:
I was playing around with AutoLayout to fix this issue but was never able to get it to work.

Related

How to present UIView from UITableViewCell

In my app I have a UITableViewCell with a button in it.
- (void)buttonPressed:(id)sender {
UIView *test = [[UIView alloc] initWithFrame:CGRectMake(self.frame.origin.y+5, self.frame.origin.x+53, 200, 200)];
test.backgroundColor = [UIColor redColor];
test.layer.cornerRadius = 15.0f;
test.layer.masksToBounds = YES;
[self addSubview:test];
}
However, this red view is presented behind the section headers. What's the best way to add it to the view from inside the UITableViewCell in a proper way?
Get the cell you want to add it to, and then use
[cell.contentView addSubview: test];
You can get the cell from the button's frame.origin by converting the point to the table view's coordinate system and using the UITableView method indexPathForRowAtPoint. That code might looks like this:
- (NSIndexPath *) indexPathForView: (UIView*) view;
{
CGPoint viewCenter = CGPointMake(
CGRectGetMidX(view.bounds),
CGRectGetMidY(view.bounds)
);
viewCenter = [self.tableView convertPoint: viewCenter fromView: view];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: viewCenter];
return indexPath;
}
Or better yet, as an extension on UITableView:
#implementation UITableView (indexPathForCellContainingView)
- (NSIndexPath *) indexPathForCellContainingView: (UIView *) view;
{
//get the view's center point (which will be in it's parent view's coordinate system
//And convert it to the table view's coorindate system
CGPoint viewCenter = [self convertPoint: view.center fromView: view.superview];
//Ask the table view which cell's index path contains that point.
return [self indexPathForRowAtPoint: viewCenter];
}
#end
You can try this:
- (void)buttonPressed:(id)sender {
UIView *test = [[UIView alloc] initWithFrame:CGRectMake(5, 53, 200, 200)];
test.backgroundColor = [UIColor redColor];
test.layer.cornerRadius = 15.0f;
test.layer.masksToBounds = YES;
[self.contentView addSubview:test];
}

iOS - Facebook POP: slide tableview cell to bottom right corner

Maybe you can help me with a problem.
I'm using the new Facebook POP animation framework in an iOS (7) app.
I have a tableview with a "+ button" in each cell. I want that if a user clicks on a button in the cell, that the cell (a copy of that cell) slides to the bottom right corner (in the last tabbar item) from alpha 1 to 0. Like a "add to cart" button. So the user knows that the row is added to the cart.
Does anyone know how I can accomplish that with the Facebook POP framework? Or can you point me in the right direction?
I think it's not so difficult, but I can't figure it out.
Many thanks in advance!
Assuming you refer to a UITableView inside a UIViewController which is assigned as a tab of a UITabBarController, first you duplicate the selected cell into a UIView and then you perform basic POP animation as follows:
#import <math.h>
#import "POPAnimation.h"
#import "POPBasicAnimation.h"
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIView *view = [self duplicateCell:cell
ContentOffset:tableView.contentOffset
Row:indexPath.row];
[self.view addSubview:view];
NSUInteger numberOfTabs = 3;
CGFloat tabWidth = self.view.frame.size.width / numberOfTabs;
[self fadeOutView:view WhileMoveTo:CGRectMake(self.view.frame.size.width - tabWidth,
tableView.frame.size.height,
tabWidth,
view.frame.size.height)];
}
- (UIView*)duplicateCell:(UITableViewCell*)cell ContentOffset:(CGPoint)offset Row:(NSInteger)row
{
CGFloat cellHeight = cell.frame.size.height;
CGFloat topVisibleCellRow = (int)(offset.y / cellHeight) ;
CGFloat delta = fmodf(offset.y, cellHeight);
CGRect frame = cell.frame;
frame.origin.y = (row - topVisibleCellRow)*cellHeight - delta;
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [UIColor yellowColor];
UILabel *label = [[UILabel alloc] initWithFrame:cell.textLabel.frame];
label.textColor = [UIColor blackColor];
label.text = cell.textLabel.text;
label.contentMode = UIViewContentModeScaleAspectFit;
[view addSubview:label];
return view;
}
- (void)fadeOutView:(UIView*)view WhileMoveTo:(CGRect)rect
{
[view pop_removeAllAnimations];
POPBasicAnimation *animFrame = [POPBasicAnimation animationWithPropertyNamed:kPOPViewFrame];
POPBasicAnimation *animAlpha = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
CGFloat fDuration = 1.5f;
animFrame.toValue = [NSValue valueWithCGRect:rect];
animFrame.duration = fDuration;
animAlpha.toValue = #(0.0);
animAlpha.duration = fDuration;
[view pop_addAnimation:animFrame forKey:#"animateFrame"];
[view pop_addAnimation:animAlpha forKey:#"animateAlpha"];
}
This example is based on a basic UITableViewCell but you can adapt the cell duplication to any custom cell scheme.

UICollectionView full screen zoom on UICollectionViewCell

How can you zoom in on a UICollectionViewCell so that it will be displayed full screen? I have extended UICollectionViewFlowLayout and in my view controller when a cell is tapped I'm doing this:
CGPoint pointInCollectionView = [gesture locationInView:self.collectionView];
NSIndexPath *selectedIndexPath = [self.collectionView indexPathForItemAtPoint:pointInCollectionView];
UICollectionViewCell *selectedCell = [self.collectionView cellForItemAtIndexPath:selectedIndexPath];
NSLog(#"Selected cell %#", selectedIndexPath);
Not really sure where to go from here. Should the UICollectionView be responsible of showing the zoomed in cell? Or should I create a new view controller that displays the content of the cell (an image) in full screen?
I took the solution here and modified it slightly to work with a collection view instead. I also added a transparent gray background to hide the original view a bit (assuming the image doesn't take up the entire frame).
#implementation CollectionViewController
{
UIImageView *fullScreenImageView;
UIImageView *originalImageView;
}
...
// in whatever method you're using to detect the cell selection
CGPoint pointInCollectionView = [gesture locationInView:self.collectionView];
NSIndexPath *selectedIndexPath = [self.collectionView indexPathForItemAtPoint:pointInCollectionView];
UICollectionViewCell *selectedCell = [self.collectionView cellForItemAtIndexPath:selectedIndexPath];
originalImageView = [selectedCell imageView]; // or whatever cell element holds your image that you want to zoom
fullScreenImageView = [[UIImageView alloc] init];
[fullScreenImageView setContentMode:UIViewContentModeScaleAspectFit];
fullScreenImageView.image = [originalImageView image];
// ***********************************************************************************
// You can either use this to zoom in from the center of your cell
CGRect tempPoint = CGRectMake(originalImageView.center.x, originalImageView.center.y, 0, 0);
// OR, if you want to zoom from the tapped point...
CGRect tempPoint = CGRectMake(pointInCollectionView.x, pointInCollectionView.y, 0, 0);
// ***********************************************************************************
CGRect startingPoint = [self.view convertRect:tempPoint fromView:[self.collectionView cellForItemAtIndexPath:selectedIndexPath]];
[fullScreenImageView setFrame:startingPoint];
[fullScreenImageView setBackgroundColor:[[UIColor lightGrayColor] colorWithAlphaComponent:0.9f]];
[self.view addSubview:fullScreenImageView];
[UIView animateWithDuration:0.4
animations:^{
[fullScreenImageView setFrame:CGRectMake(0,
0,
self.view.bounds.size.width,
self.view.bounds.size.height)];
}];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(fullScreenImageViewTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[fullScreenImageView addGestureRecognizer:singleTap];
[fullScreenImageView setUserInteractionEnabled:YES];
...
- (void)fullScreenImageViewTapped:(UIGestureRecognizer *)gestureRecognizer {
CGRect point=[self.view convertRect:originalImageView.bounds fromView:originalImageView];
gestureRecognizer.view.backgroundColor=[UIColor clearColor];
[UIView animateWithDuration:0.5
animations:^{
[(UIImageView *)gestureRecognizer.view setFrame:point];
}];
[self performSelector:#selector(animationDone:) withObject:[gestureRecognizer view] afterDelay:0.4];
}
-(void)animationDone:(UIView *)view
{
[fullScreenImageView removeFromSuperview];
fullScreenImageView = nil;
}
You can simply use another layout (similar to the one you already have) wherein the item size is larger, and then do setCollectionViewLayout:animated:completion: on the collectionView.
You don't need a new view controller. Your datasource remains the same. You can even use the same cell Class, just make sure that it knows when to layout things for a larger cell content size, and when not to.
I'm quite sure that's how Facebook does it in Paper, as there is no reloading of the content, i.e. [collectionView reloadData] never seems to be called (would have caused flickering and resetting of the scroll offset, etc). This seems to be the most straight forward possible solution.
CGPoint pointInCollectionView = [gesture locationInView:self.collectionView];
NSIndexPath *selectedIndexPath = [self.collectionView indexPathForItemAtPoint:pointInCollectionView];
UICollectionViewCell *selectedCell = [self.collectionView cellForItemAtIndexPath:selectedIndexPath];
NSLog(#"Selected cell %#", selectedIndexPath);
__weak typeof(self) weakSelf = self;
[self.collectionView setCollectionViewLayout:newLayout animated:YES completion:^{
[weakSelf.collectionView scrollToItemAtIndexPath:selectedIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}];
You can use MWPhotoBrowser, which is suitable for your problem. It supports Grid with Tap to Zoom functionality. you can get it from here
Grid
In order to properly show the grid of thumbnails, you must ensure the property enableGrid is set to YES, and implement the following delegate method:
(id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index;
The photo browser can also start on the grid by enabling the startOnGrid property.

Making a list of UIViews that slide up and down when touched

I'm trying to figure out an approach to build something like the image below, which is a list of items that when a section is clicked slides out content. It's a really common UX on most websites and what not. My idea is to have each gray box (button) slide out a UIView containing some other items. I'm still new to iOS development but I'm struggling to find how you can animate a UIView to slide down and push the content below it down as well. Hoping some one can give me a good starting point or point to some info outside the realm of the apple docs.
Thanks!
So if you just have a few views, I would not recommend the UITableView approach, since it is not so easy to customize with animations and table views usually want to fill the whole screen with cells. Instead write a expandable UIView subclass that has the desired two states. Add a method to switch between extended and collapsed state. On expanding/collapsing adjust their positions so that they always have enough space.
I provide you an example of views adjusting their frames. I guess it should be easy to do the same with auto layout constraints: give the views a fixed height constraint and change this on collapsing/expanding. The same way set the constraints between the views to be 0 so that they are stacked on top of each other.
Expandable View:
#interface ExpandingView(){
UIView *_expandedView;
UIView *_seperatorView;
BOOL _expanded;
}
#end
#implementation ExpandingView
- (id)init
{
self = [super initWithFrame:CGRectMake(15, 0, 290, 50)];
if (self) {
_expanded = NO;
self.clipsToBounds = YES;
_headerView = [[UIView alloc] initWithFrame:self.bounds];
_headerView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
[self addSubview:_headerView];
_seperatorView = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-1, self.bounds.size.width, 1)];
_seperatorView.backgroundColor = [UIColor lightGrayColor];
[self addSubview:_seperatorView];
_expandedView = [[UIView alloc] initWithFrame:CGRectOffset(self.bounds, 0, self.bounds.size.height)];
_expandedView.backgroundColor = [UIColor blueColor];
[self addSubview:_expandedView];
}
return self;
}
- (void)layoutSubviews{
[self adjustLayout];
}
- (void)adjustLayout{
_headerView.frame = CGRectMake(0, 0, self.bounds.size.width, 50);
_seperatorView.frame = CGRectMake(0, 49, self.bounds.size.width, 1);
_expandedView.frame = CGRectMake(0, 50, self.bounds.size.width, self.bounds.size.height-50);
}
- (void)toggleExpandedState{
_expanded = !_expanded;
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, _expanded?200:50);
[self adjustLayout];
}
#end
ViewController:
#interface ExpandingViewController (){
NSArray *_expandingViews;
}
#end
#implementation ExpandingViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_expandingViews = #[
[[ExpandingView alloc] init],
[[ExpandingView alloc] init],
[[ExpandingView alloc] init],
];
for(ExpandingView *view in _expandingViews){
[view.headerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(expandingViewTapped:)]];
[self.view addSubview:view];
}
}
- (void)viewWillLayoutSubviews{
int y = 100;
for(ExpandingView *view in _expandingViews){
view.frame = CGRectOffset(view.bounds, (CGRectGetWidth(self.view.bounds)-CGRectGetWidth(view.bounds))/2, y);
y+=view.frame.size.height;
}
}
- (void)expandingViewTapped:(UITapGestureRecognizer*)tapper{
ExpandingView *view = (ExpandingView*)tapper.view.superview;
[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0 options:0 animations:^{
[view toggleExpandedState];
[self.view layoutIfNeeded];
} completion:nil];
}

UIView animate alpha when view is offscreen doesn't work

I have a UIView which is located offscreen and I'm animating the frame so that the view slides in offscreen from the bottom and is visible. I'd like to simultaneously animate the alpha property of a UILabel on the view as well so it fades in. Unfortunately it appears I can't do the alpha animation because the view is offscreen and doesn't appear to take hold. It looks something like this:
nextCell.titleLabel.alpha = 0;
[UIView animateWithDuration:collapsedAnimationDuration animations:^{
CGRect newFrame = lastCell.frame;
newFrame.origin = CGPointMake(lastCell.frame.origin.x , lastCell.frame.origin.y + THREAD_CELL_HEIGHT);
nextCell.frame = newFrame;
nextCell.titleLabel.alpha = 1;
}];
Is it not possible to start animating the alpha of the subview because it's offscreen? If I position the view on screen and then try the animation it looks great but that's not the effect I'm going for. Thanks for your help.
Is this code executed in cellForRowAtIndexPath? If so, try moving it to tableView:willDisplayCell:forRowAtIndexPath:. The table view resets various properties of the cell before displaying it.
From the AppDelegate didFinishLaunching method:
self.myView = [[MyView alloc] initWithFrame:CGRectMake(320, 480, 400, 400)];
self.myView.titleLabel.text = #"test text";
self.myView.titleLabel.alpha = 0;
[UIView animateWithDuration:10.0 animations:^{
CGRect newFrame = self.myView.frame;
newFrame.origin = CGPointMake(0 , 0);
self.myView.frame = newFrame;
self.myView.titleLabel.alpha = 1;
}];
[self.viewController.view addSubview:self.myView];
MyView is just this:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self addSubview:self.titleLabel];
}
return self;
}
- (UILabel *)titleLabel
{
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
}
return _titleLabel;
}
I did no important changes to the code you presented and it worked fine. So assuming you're not doing what Tim mentioned (it won't work if you're doing it), we need more details to help you out.

Resources