IOS: scrollView with a paging random - ios

I have a scrollView and inside this scroll view I have 4 images. It's in paging enabled. But I want to do an IBAction that scroll these images random. How can I do?

#define numPages 4
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * (arc4random() % (numPages - 1));
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];

it may be useful to you.
every time change the contentoffset
[scrollView contentOffset:CGSizeMake(totalscrollviewwidth,480];
And find this link also

Related

fade/show UIButton when scroll down/up

I have a tableView. on view is UIButton. i want when scroll down button fade and when scroll stop or up button show.
code:
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(#"Start scroll");
//I assume the buttons are within your cells so you will have to enable them within your cells so you will probably have to handle this by sending a custom message to your cells or accessing them with properties.
CGRect frame = addCommentBtn.frame;
frame.origin.y = scrollView.contentOffset.y + self.newsTableView.frame.size.height + 4;
addCommentBtn.frame = frame;
[self.view bringSubviewToFront:addCommentBtn];}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(#"End scroll");
// do the same to enable them back
CGRect frame = addCommentBtn.frame;
frame.origin.y = scrollView.contentOffset.y + self.newsTableView.frame.size.height + 4;
addCommentBtn.frame = frame;
[self.view bringSubviewToFront:addCommentBtn];}
but don't work correctly!
Thanks
You are using wrong method. Instead of
scrollViewWillBeginDecelerating: use scrollViewDidScroll: and update button alpha and position based on contentOffset of the scroll view.

Implementing iPad General Setting Page

So far I have customized the tableview and implemented the iPad General Setting Page. Below is the code for tableview which will change frame accordingly. But the issue is when I insert/delete rows or section in the tableview. My tableviewcell backgroundview (not cell) width get shrinks. Any idea what wrong am I doing here?
- (void)setFrame:(CGRect)frame
{
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
CGFloat inset =10;
frame.origin.x += inset;
frame.size.width -= 2 * inset;//The issue is in this line
}
[super setFrame:frame];
}
I found the simple solution to achieve this. No need of customizing UITableView subclass. Just take the outlet of tableview and set the frame and change the color of backgroundview. like that below:-
CGFloat tableBorderLeft = 10;
CGFloat tableBorderRight = 10;
CGRect tableRect = self.view.frame;
tableRect.origin.x += tableBorderLeft; // make the table begin a few pixels right from its origin
tableRect.size.width -= tableBorderLeft + tableBorderRight; // reduce the width of the table
yourTableView.frame = tableRect;
self.view.backgroundColor=[UIColor colorWithRed:(239/255.0f) green:(239/255.0f) blue:(244/255.0f) alpha:1.0];

UIScrollview imbeded within UIPageviewController resizes after page turned

So I have a UIPageviewController used that contains various news articles. The articles are presented on UIWebviews of varying lengths, and each UIWebview is contained within a UIScrollview. When the first article is loaded, everything works, and the user is able to scroll up and down the page and read the entire article. However, when the user swipes to the left or right to view the next article, the UIScrollview on that page is resized to only be the height of the screen and thus will only scroll enought to bounce back. I can tell that the UIWebview is longer than what is being shown because I have printed it's height, and when scrolling, you can see that the article continues. Also, when you swipe back to the initial article View, this UIScrollview is also resized to only be the screen size.
The UIWebView has it's content's loaded in the ArticleViewController's init method, and then is sized within the webViewDidLoad method like this:
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
This seems to work, although tell me if you think I should change it.
To get to each article, you click on it from a UITableview presenting each of the articles. Within the DidSelectRowAtIndexPath method for this, I set up the UIPageViewController like so:
UIPageViewController *articlesPageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
articlesPageViewController.dataSource = self;
articlesPageViewController.delegate = self;
NVM! The Solution was turning off auto layout for the UIScrollview! Thanks everyone!
(void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame; NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
}
Get height of the webview then set the content size of the scrollview
OR try this way:
CGFloat height = [[myWebView stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
CGFloat width = [[myWebView stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
CGRect frame = myWebView.frame;
frame.size.height = height + 125.0;
frame.size.width = width;
myWebView.frame = frame;
mainScrollView.contentSize = myWebView.bounds.size;

UIScrollView Direction RTL for Right to Left Languages

Can we change the behavior of UIScrollView that it scrolls RTL content in it's reverse mode.
You can achieve it by rotating UIScrollView in Child views by 180 (Pi).
self.scrollView.transform = CGAffineTransformMakeRotation(M_PI);
subView.transform = CGAffineTransformMakeRotation(M_PI);
count = 6;
[self.scrollView setFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, 320, 480)];
[pageControl setNumberOfPages:count];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * count, self.scrollView.frame.size.height);
for(int i=count-1; i>=0; i--) { //start adding content in reverse mode
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * (count - i - 1); //ar
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
[imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%i.png", i]]];
[self.scrollView addSubview:imageView];
}
//scroll to the last frame as it's the first page for RTL languages
CGRect frame;
frame.origin.x = scrollView.frame.size.width * (count - 1);
frame.origin.y = 0;
frame.size = scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:NO];
The page control also needs to be indicate the last dot as the first dot (or first page)
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = (page % count);
}
I try it for xCode 7 and iOS 9 work good.
[self._imagesScrollView setTransform:CGAffineTransformMakeScale(-1, 1)];
I m not sure if I understand the question correctly but I take it like this: You want to have a (let's say) horizontal UIScrollView where you can add elements from right to left and when the contents exceed the scroll view frame you want to be able to scroll to the left rather than to the right in order to see the last elements.
In this case you can do this. For every new element calculate the total width you need and set it as contentOffset:
[scrollView setContentSize:CGSizeMake(self.childView.frame.size.width,
scrollView.frame.size.height)];
Above I have a childView where the elements are added. It is placed at the right most position inside the scrollView minus 1 element width. Every time an element is added call:
[scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x + kElementWidth,0.0)];
which will offset everything to the left. Then:
self.videoQueueCollectionView.center = CGPointMake(self.childView.center.x + kElementWidth,
self.childView.center.y);
That will snap eveything back to where it was BUT with the contentOffset set to the left. Which means that you can now scroll to the right.
I use this code for Swift 3
self.scrollView.transform = CGAffineTransform(scaleX:-1,y: 1);
You can achieve this by setting semanticContentAttribute
self.scrollView.semanticContentAttribute = .forceRightToLeft

IOS: random paging scrollview

I have this code for a scrollView (with paging enabled)
- (IBAction) random{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * (arc4random() % (numberOfPage - 1));
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];}
It make a random paging scroll and it's ok, but it is very slow. Can I have a fast scrolling as a slot machine?
I'm not sure you could get it to look like slot machine (you'd probably need some motion blur to get that kind of effect), but you could try putting your scrollRectToVisible: in an animation block to change the duration of the animation:
- (IBAction) random
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * (arc4random() % (numberOfPage - 1));
frame.origin.y = 0;
[UIView animateWithDuration: <your duration>
animations:^(void) {
[scrollView scrollRectToVisible:frame animated: NO];
}];
}
Don't forget the animated:NO, otherwise it'll override your duration

Resources