There is a way to customize timing on animations done via performBatchUpdates?
I have this code
[self.stepCollectionView performBatchUpdates:^{
self.stepSelectedIndex = indexPath.row;
UICollectionViewCell *cell = [self.stepCollectionView cellForItemAtIndexPath:indexPath];
[UIView transitionWithView:cell
duration:0.5f
options: UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionBeginFromCurrentState
animations:^{
CGRect frame = cell.frame;
frame.size.height = 416;
cell.frame = frame;
}
completion:^(BOOL finished) {
}];
} completion:^(BOOL finished) {
}];
I would change the height of the UICollectionViewCell and at the same time reorganize the subviews of the UICollectionViewCell.
There is a way you can customise the duration and the Timing function for UICollectionView's batch update. Try the below code. And here is how it looks https://youtu.be/R0VdC4dliPI
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[CATransaction begin];
[CATransaction setAnimationDuration:0.3];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithControlPoints:0 :1.8 :0 :1]];
[self.menuCollectionView performBatchUpdates:^{
[self.menuCollectionView insertItemsAtIndexPaths:#[[NSIndexPath indexPathForItem:index inSection:0]]];
[animatableMenuItems insertObject:#"" atIndex:index];
} completion:^(BOOL finished) {
}];
[CATransaction commit];
[UIView commitAnimations];
Find the full source code for the example at https://github.com/DoddaSrinivasan/MultiRowUITabBar
Yes you can change the duration of this animation.
Change the layer.speed property of your UICollectionView.
Just wrap it up in a -[UIView animateWithDuration:animations:] block with your desired animation. It's probably not the correct way to do it, but it seems to work fine for me with iOS 8.
No, there is no way to customize the timing of the animation caused by performBatchUpdates. It looks like you are trying to animate just on Cell in particular in your collectionview. Why not just use + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations instead?
Related
I'm trying to run an example to test this function. I have a label an a button in the storyBoard and I have referenced the bottom constraint of the label in the view controller. When I use the button I want the label to move with an animation but it moves without it. Here's the code of the button:
- (IBAction)sd:(id)sender {
[UIView animateWithDuration:0.5f animations:^{
self.constraint.constant += 50;
}];
}
I know how to use it in swift but I'm having problems in objective c and I know it will be just for a little mistake... Any help?
This is not the right way of animating a UI component constrained with Autolayout. You should first update the constraint, then call layoutIfNeeded within the animation block:
self.constraint.constant += 50;
[UIView animateWithDuration:0.5f animations:^{
[self.view layoutIfNeeded];
}];
Use this
self.constraint.constant += 50;
[UIView animateWithDuration:0.5f
animations:^{
[self.view layoutIfNeeded];
}];
Try this setNeedsLayout helps in some cases.
self.constraint.constant += 50;
[UIView animateWithDuration:0.5f
animations:^{
[self.view setNeedsLayout];
}
completion:^(BOOL finished) {
}];
I have one one collectionview in that I have 10 images loaded from webservice. I want to autoscroll that imageview from 1st to last position and then last to 1st position continuously whenever that page appears.
I use below method
-(void)scrollSlowly
{
CATransition *transition = [CATransition animation];
transition.duration = 4.0f;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[UIView animateWithDuration:5.0f
delay:3.0f
options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
animations:^{
[self.CollectionView setContentOffset:CGPointMake(CGFLOAT_MAX ,0)];
}
completion:nil];
[self.CollectionView.layer addAnimation:transition forKey:#"transition"];
}
-(void)scrollSlowlyToPoint
{
self.CollectionView.contentOffset = self.scrollingPoint;
// Here you have to respond to user interactions or else the scrolling will not stop until it reaches the endPoint.
if (CGPointEqualToPoint(self.scrollingPoint, self.endPoint))
{
[self.scrollingTimer invalidate];
}
// Going one pixel to the right.
self.scrollingPoint = CGPointMake(self.scrollingPoint.x+1, self.scrollingPoint.y);
}
I want to autoscroll horizontally in Objective-C. thanks in advance
Create a custom UICollectionViewCell class, create and connect an outlet for image view.
In .h file:
NSMutableArray *imgArr;
NSInteger testIndexPath;
I guess you can implement the datasource methods for UICollectionView.
In cellForItemAtIndexPath add this line after adding images to the imageview
testIndexPath=indexPath.row;//this will give you the indexPath for cell
Now add this two methods in your .m file:
-(void)autoScroll{
CGFloat point = self.collectionVw.contentOffset.x;
CGFloat lo = point + 1;
[UIView animateWithDuration:0 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.collectionVw.contentOffset = CGPointMake(lo, 0);
}completion:^(BOOL finished){
if (testIndexPath==8)
{
[self autoScrollReverse];
}
else{
[self autoScroll];
}
}];
}
-(void)autoScrollReverse{
CGFloat point = self.collectionVw.contentOffset.x;
CGFloat lo = point - 1;
[UIView animateWithDuration:0 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.collectionVw.contentOffset = CGPointMake(lo, 0);
}completion:^(BOOL finished){
if(testIndexPath == 0){
[self autoScroll];
}else{
[self autoScrollReverse];
}
}];
}
//Call [self autoScroll] in your viewDidLoad
Better way to scroll index from top to bottom and bottom to top.
This will move to index 10 means scroll top to bottom
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:9 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
For again scroll bottom to top which move to index 0
[UIView animateWithDuration:3
delay:0.1
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}
completion:nil];
You can also set scrolling from top to bottom in UIVIew animation like above.
Set the first method in ViewWillAppear and set second method in ViewDidAppear if you need animation for every time page appear
Please apply this one. May be its help to you
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"MyCollectionViewCell" forIndexPath:indexPath];
[UIView animateWithDuration:0.2 animations:^{
cell.transform = CGAffineTransformMakeTranslation(0.0, -50);
}];
[self delayBy:0.5 code:^{ // call block of code after time interval
[UIView animateWithDuration:0.3 animations:^{
cell.transform = CGAffineTransformIdentity;
}];
}];
return cell;
}
This may help solve your problem:
int maxDelay = 4;
int delayInSeconds = arc4random() % maxDelay;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//your codes//
});
I'm trying a simple animation for selection/deselection of a UITableViewCell like this:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[UIView animateWithDuration: 0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
tabConstraint.constant = selected ? 40 : 20;
} completion:nil];
}
The code inside the animations block will be called, but it's not animating. Everything works fine, but there's not any animation at all. How could I make the cell selection animated?
Every time you update a autolayout constraint, you have to call layoutIfNeeded,
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[UIView animateWithDuration: 0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
tabConstraint.constant = selected ? 40 : 20;
[self layoutIfNeeded];
} completion:nil];
}
You need to call layoutIfNeeded in your animations block. Check out the accepted answer on this question for more details: How do I animate constraint changes?
You need to call layoutIfNeeded() inside the animation block :-)
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
tabConstraint.constant = selected ? 40 : 20;
[UIView animateWithDuration: 0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
whateverTabYouHaveHere.layoutIfNeeded()
} completion:nil];
}
I have the following code
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
mainPage.frame=CGRectMake(0, -[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height,[UIScreen mainScreen].bounds.size.width);
} completion:nil];
Is there any way to control the animation duration of the subviews of mainPage.
You can use UIView:animateKeyframesWithDuration then use UIView:addKeyframeWithRelativeStartTime to set its start time and duration
[UIView animateKeyframesWithDuration:2 delay:0 options:0 animations:^{
[UIView addKeyframeWithRelativeStartTime:0.32 relativeDuration:0.68 animations:^{
view.frame = frame;
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:1 animations:^{
subview.frame = frame2;
}];
} completion:^(BOOL finished) {
}];
If you want the subviews to animate with a different duration then you'll need to animate each of them separately. You can loop through the mainPage.subviews array and issue an animateWithDuration:delay:options:animations:completion: call to each subview in turn, each with a different duration.
I have a UIView and I am animating it.
The thing here is that I need to release theView once it is animated out.
originalRECT = [[UIScreen mainScreen] bounds];
if(theView)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
theView setFrame = originalRECT;
[UIView commitAnimations];
[theView autorelease];
theView = nil;
}
So I know the code is setting theView to nil, but the animation does finish ok (No SIGABRT or something like that)
Or is there a callback function I can use to know that the view disappeared? How can I use a function like that in this case?
Thanks!
Few things -
First of all if you want to be sure you can use:
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationCurveEaseInOut
animations:^ {
//your animation
theView setFrame = originalRECT;
}
completion:^(BOOL finished) {
//Animation finished you can release
[theView release];
}];
second - why do you use autoRelease and not release?
[theView release];
Third - Just for w meeter you don't know. USE ARC it will make your life a bit easier. You can read more here ARC tutorial