IOS: random paging scrollview - ios

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

Related

UIScrollView doesn't scroll with method scrollRectToVisible

I have a question for you!
I have a method:
- (void)pushNewTableViewController:(NewTableViewController *)newTableViewController
{
[self addChildViewController:newTableViewController];
[self.scrollView addSubview:newTableViewController.view];
[UIView animateWithDuration:0.5f animations:^
{
CGRect frame = CGRectZero;
frame.origin.x = COLUMN_WIDTH * self.navigationStack.count;
frame.origin.y = 0.0f;
frame.size.width = COLUMN_WIDTH;
frame.size.height = self.view.frame.size.height;
newTableViewController.view.frame = frame;
}
completion:^(BOOL finished)
{
[self.navigationStack addObject:newTableViewController];
self.scrollView.contentSize = [self sizeForContent];
[self.scrollView scrollRectToVisible:newTableViewController.view.frame animated:YES];
}];
}
Basically, this method works fine, except the line [self.scrollView scrollRectToVisible:newTableViewController.view.frame animated:YES];.
The idea of this method is to scroll the scroll view to the right to make newTableViewController.view.frame to be fully visible. But instead of this, scroll view doesn't scroll anywhere...
Maybe any one of you, guys, know why it doesn't work as I expect?

Permanently move a frame of custom UIView on screen

I'm having a little problem about animations on iOS with Objective-c.
I'm trying to create a card game, and as the game start, the deck distributes the cards on the table, with an animation. The problem is that at the beginning the cards are all in row in the bottom side of the screen, then with the animation they distribute all over the table. But when I tap on anyone of them, they return in their initial position, although the game keeps on working because they flip and unflip over, as well as the score works properly. I'm attaching some snippets of the code and of the view.
Here are links to screenshots:
1)cards as the game starts: prntscr.com/68r8yj
2)cards at the end of initial animation: prntscr.com/68r92u
3)cards as I tap on any of them: prntscr.com/68r2hr
Here's my code snippet
-(void)viewDidAppear:(BOOL)animated {
CGPoint startPoint = {21, 34};
for(int i = 0; i < [self.cardsView count]; i++) {
FrenchPlayingCardView *view = [self.cardsView objectAtIndex:i];
int width = view.bounds.size.width;
int height = view.bounds.size.height;
CGRect newFrame = CGRectMake(startPoint.x, startPoint.y, width, height);
/*[UIView animateWithDuration:1.0
delay:1.0
options:UIViewAnimationOptionTransitionNone
animations:^(){
view.frame = newFrame;;
}
completion:nil];*/
[UIView beginAnimations:#"move" context:nil];
[UIView setAnimationDuration:3.0];
view.frame = newFrame;
[UIView commitAnimations];
NSLog(#"index:%d %f %f", i, view.frame.origin.x, view.frame.origin.y);
startPoint.x += 76;
if(i % 4 == 3) {
startPoint.y += 100;
startPoint.x = 21;
}
}
[super viewDidAppear:animated];
}
call [super viewDidAppear] before your animation and close autolayout. I think autolayout can cause this

Set position of UIPickerView at bottom even after scroll in ios

I am trying to show UIPickerView on click on UITextField. I am using a simple method to show the picker as follows:
-(void) showPickerView {
[self.view resignFirstResponder];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.50];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if(iOSDeviceScreenSize.height == 480){
CGRect frame = self.picker.frame;
frame.origin.y = 270;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 226.0;
self.toolBar.frame = frame;
} else if(iOSDeviceScreenSize.height == 568){
CGRect frame = self.picker.frame;
frame.origin.y = 239.0;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 195.0;
self.toolBar.frame = frame;
}
[UIView commitAnimations];
}
It works great if I have not scrolled down. But when I scroll down, my picker is placed on fix position as mentioned in showPickerView method. Now how I can manage it that my picker appears at bottom every time.
Solution 1:
Add your pickerview to your window.
Solution 2:
Implement the scrollview delegate and adjust the frame continuously.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if(iOSDeviceScreenSize.height == 480){
CGRect frame = self.picker.frame;
frame.origin.y = 270;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 226.0;
self.toolBar.frame = frame;
} else if(iOSDeviceScreenSize.height == 568){
CGRect frame = self.picker.frame;
frame.origin.y = 239.0;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 195.0;
self.toolBar.frame = frame;
}
}
I can just suppose that you are showing your picker inside of a scroll view, right?
And if so, then your picker frame is relative to UIScrollView, not to a screen view.
So you have to take into account your scroll view offset. Something like that:
CGRect frame = self.picker.frame;
frame.origin.y = self.scrollvew.contentOffset.y + 270;
Otherwise you can show your picker outside of the UIScrollView (just add it to a screen view).

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: scrollView with a paging random

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

Resources