iOS - UIScrollView from half to full screen - ios

In a UIViewController, I have a UIScrollView that takes half of the screen. This UIScrollView contains a collection of UIView. On some specific event, such as a swipe, I want my UIScrollView to take full screen animatedly, how do I achieve this behavior?

Try this...
// adding swipe gesture to your scrollview
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
// Set swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[scrollview addGestureRecognizer:swipeLeft];
// add gesture action method
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
// if you are having only one swipe gesture, you no need to add the below condition. you can add the code inside the condition
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(#"Left Swipe");
scrollView.contentSize = self.view.frame;
scrollView.frame = self.view.frame;
}
}

Try using this method on event and set :
scrollView.contentSize = CGSizeMake(847, 800); // change on basis of your requirement
For animating scroll view :
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated; // animate at constant velocity to new offset

Quoting from #RAJA's answer and slight improvement:
[UIView beginAnimations:#"whatever" context:nil];
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * <count of subviews>);
scrollView.frame = self.view.frame;
[UIView commitAnimations];

I just extend the first answer, If you want animate the above process, you can achieve that by appending the following code :
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe
{
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
{
[UIView animateWithDuration:0.15f // set duration for your animation here
animations:^{
scrollView.contentSize = self.view.frame;
scrollView.frame = self.view.frame;
}
completion:^(BOOL finished){
NSLog(#"completion block");
}];
}
}

Related

UIView not animated properly using UIView Animation

I am working on UIView Animation. I want to move a view upside while i scroll up and should move down when i scroll down.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint scrollVelocity = [_collectionViewLeaderboard.panGestureRecognizer velocityInView:_collectionViewLeaderboard.superview];
if (scrollVelocity.y > 0.0f){
NSLog(#"going down");
[UIView animateWithDuration:0.3f
animations:^ {
_headerview.frame = CGRectMake(0, 0, _headerview.frame.size.width, _headerview.frame.size.height);
_headerviewSecond.frame = CGRectMake(0, _headerview.frame.size.height, _headerviewSecond.frame.size.width, _headerviewSecond.frame.size.height);
self.collectionViewLeaderboard.frame = CGRectMake(self.view.frame.origin.x, _headerviewSecond.frame.size.height+_headerview.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
frameconditon = _headerview.frame;
_viewOptions.frame = CGRectMake(_viewOptions.frame.origin.x,_headerview.frame.size.height+_headerviewSecond.frame.size.height+30, _viewOptions.frame.size.width, _viewOptions.frame.size.height);
} completion:^ (BOOL completed) {
}];
}
else if (scrollVelocity.y < 0.0f){
NSLog(#"going up");
[UIView animateWithDuration:5.3 animations:^{
_headerview.frame = CGRectMake(_headerview.frame.origin.x, -(_headerview.frame.size.height), _headerview.frame.size.width, _headerview.frame.size.height);
_headerviewSecond.frame = CGRectMake(0, (_headerview.frame.size.height)-40, _headerviewSecond.frame.size.width, _headerviewSecond.frame.size.height);
self.collectionViewLeaderboard.frame = CGRectMake(self.view.frame.origin.x, _headerviewSecond.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
frameconditon = _headerviewSecond.frame;
_viewOptions.frame = CGRectMake(_viewOptions.frame.origin.x,_headerviewSecond.frame.size.height+20, _viewOptions.frame.size.width, _viewOptions.frame.size.height);
} completion:^(BOOL finished) {
}];
}
}
I believe its very simple thing if you want to move the view up and down.
Because If you add view in scroll view you can automatically get the view up and down along with the scroll. You just need to set "contentSize" property of scrollView.
If its not the case, you are trying to get panGesture of UICollectionView then you can not get in "scrollViewDidScroll" method.
Here is the useful tutorial on UIPanGestureRecognizer with UICollectionView. Hope so it will help.
https://uncorkedstudios.com/blog/using-uigesturerecognizers-with-uicollectionviews

Cant find the Scroll View property to know how many pixels the scroll view did scroll

I have a scroll view with a label in it, and I wants after someones scroll the label X px's to the right and release his finger to delete this label with animation.
So I created a delegate connection and added the scroll view delegate method:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(#"scroll view did scroll");
}
In this method I want to say something like:
if myScrollView.someProperty moved X px's to the right and the user pulled his finger
delete this label sliding with animation to the right
Can someone please help out here :/
tnx ahead!!
Check UIScrollView's contentOffset property:
contentOffset - The point at which the origin of the content view is offset
from the origin of the scroll view.
you can use UISwipeGestureRecognizer to do this and define how many pixel you want to drag the label to the right. You can try the following code
- (void)viewDidLoad {
[super viewDidLoad];
mylabel.userInteractionEnabled=YES;
[mylabel sizeToFit];
[mylabel addGestureRecognizer:[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipeLabel:)]];
}
- (void)didSwipeLabel:(UISwipeGestureRecognizer*)swipe
{
NSLog(#"swipe");
swipe.direction = UISwipeGestureRecognizerDirectionRight;
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
[UIView animateWithDuration:0.5 animations:^{
// swipe the label 50px right
mylabel.transform = CGAffineTransformMakeTranslation(50.0, 0.0);
} completion:^(BOOL finished) {
// when animation
[UIView animateWithDuration:0.5 animations:^{
NSLog(#"label should be removed");
[mylabel removeFromSuperview];
}];
}];
}
}

Start UIGestureRecognizer programmatically

I want to start the UIPanGestureRecognizer right after adding it to a screenshot. Because the screenshot is created through code, when an item has been highlighted, the user won't press on the screen again. So... How do I start the recognizer programmatically?
UIView *snapshot = [cell snapshotViewAfterScreenUpdates:NO];
//use the cell to map the snapshot frame to the window because this does a perfect job of accounting for table offset, etc. Other methods put the view a little to the side or way off
CGRect newFrame = snapshot.frame;
newFrame.origin = [cell convertPoint:newFrame.origin toView:self.view.window];
[snapshot setFrame:newFrame];
[HelperMethods shadowForView:cell color:[UIColor blackColor] offset:CGSizeMake(1, 1) opacity:.7 radius:snapshot.frame.size.width/4];
//[self.view addSubview:snapshot];
newFrame.origin.y -=10;
//move the frame a little to let user know it can be moved
[UIView animateWithDuration:.2 animations:^{
[snapshot setFrame:newFrame];
}];
//add a long press that kills the tap if recognized
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(userCellDragged:)];
[pan setMinimumNumberOfTouches:1];
[pan setMaximumNumberOfTouches:1];
[cell addGestureRecognizer:pan];
You can always call the method on its own.
For example, you've added the selector
- (void) userCellDragged:(UIPanGestureRecognizer)sender;
For your pan gesture recognizer.
You could call this from anywhere in the view by simply adding
[self userCellDragged:nil];
Remember to add a parameter to this method something like:
if (sender == nil) {
// Triggered programmatically
}
else {
// proceed as normal
}

Resize UIScrollView on Long Press Gesture

I have a UIScrollView at the very top (just below the navigation bar) of a UITableViewController using Storyboards. The UIScrollView's default size is 320x50. When the user holds the scrollView for at least 1 second, I want the UIScrollView to get twice as big (320x100) in order to show additional details with an animation. The UITableView right below it should animate with it if possible and move down 50.
Holding the UIScrollView for at least 1 second when it's in the bigger setting will result in the opposite animation, and the UIScrollView will animate back to its original size.
How would I do this? Thank you ahead of time! Here's what I have so far:
- (void)viewDidLoad {
[super viewDidLoad];
self.featureScrollExpanded = NO;
UILongPressGestureRecognizer* featureHold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector (longHold:)];
[featureHold setDelaysTouchesBegan : YES];
featureHold.minimumPressDuration = 0.6;
featureHold.numberOfTouchesRequired = 1;
[self.featureScrollView addGestureRecognizer:featureHold];
[featureHold release];
}
- (void)longHold:(UIGestureRecognizer*)sender {
if (self.featureScrollExpanded==NO) {
self.featureScrollExpanded=YES;
//make it bigger
}
else {
self.featureScrollExpanded=NO;
//make it smaller
}
}
Do an animation:
CGRect frame = yourScrollView.frame;
frame.size.height += 50;
[UIView animateWithDuration:0.6
animations:^{
yourScrollView.frame = frame;
} completion:^(BOOL finished) {
//do your other animation here if you want to
//or do them both together and lose this block
}
];
Or if you want it a bit more with an overview:
CGRect frame = yourScrollView.frame;
frame.size.height += 50;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.6];
[self.yourScrollView setFrame:frame];
[UIView commitAnimations];

UIview animation overriding one view to another view

I'm new iphone developer i have problem uiview animation for ipad landscape mode.i have two view view1(like a split view) and view2(covered remaining window).when i touch move right to left view2 it will be overriding the view1 with moving animation.at same time when i touch move left to right view2 come and fit the old position.please share your ideas
With Regards,
Rajesh.
Hi all i found the answer finally,
- (void)viewDidLoad
{
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[recognizer setNumberOfTouchesRequired:1];
[secondView addGestureRecognizer:recognizer];
[recognizer release];
UISwipeGestureRecognizer *recognizerleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
[recognizerleft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[recognizerleft setNumberOfTouchesRequired:1];
[secondView addGestureRecognizer:recognizerleft];
[recognizerleft release];
[self.view addSubview:secondView];
[super viewDidLoad];
}
//To set the frame position of the view
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
NSLog(#"rightSwipeHandle");
[UIView beginAnimations:#"viewanimations" context:nil];
[UIView setAnimationDuration:0.8];
[UIView setAnimationDelegate:self];
secondView.frame=CGRectMake(360, 0, 660, 768);
[UIView commitAnimations];
}
//To set the frame position of the view
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
NSLog(#"leftSwipeHandle");
[UIView beginAnimations:#"viewanimations" context:nil];
[UIView setAnimationDuration:0.8];
[UIView setAnimationDelegate:self];
secondView.frame=CGRectMake(200, 0, 858, 768);
[UIView commitAnimations];
}
if i understand correctly, you want a sliding animation between 2 views. if so:
Make a ViewController that will hold these two views. inside this view controller's .m file define a transition method that contains the following and gets called by your buttons/other action triggers:
CGFloat windowWidth = self.mainView.frame.size.width;
CGFloat windowHeight = self.mainView.frame.size.height;
CGRect offScreenLeft = CGRectMake(-1*windowWidth, 0.0, windowWidth, windowHeight);
CGRect onScreen = self.mainView.frame;
CGRect offScreenRight = CGRectMake(windowWidth, 0.0, windowWidth, windowHeight);
if (direction == rightToLeft)
{
rightView.frame = offScreenRight;
[self.view addSubview:rightView];
[UIView animateWithDuration:0.65
animations:^{
leftView.frame = offScreenLeft;
rightView.frame = onScreen;
}
completion:^(BOOL finished){
[leftView removeFromSuperview];
}];
}else if (direction == leftToRight){
self.leftViewController.view.frame = offScreenLeft;
[UIView animateWithDuration:0.65
animations:^{
rightView.frame = offScreenRight;
leftView.frame = onScreen;
}
completion:^(BOOL finished){
[rightView removeFromSuperview];
}];
}
}
In general, make sure you are adding your subview to the view currently on screen, and make sure you set the appropriate bounds. Check that your view is not nil:
if(myView){
// not nil. thats good
}else {
// nil. this is bad. make sure myView is being initialized properly (if at all)
}
Finally, make sure neither the opacity nor the alpha property on your subview is set to 0 (you'll want these at 1 if you want it to show up completely and between 0 and 1 for a transparency effect).

Resources