Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 287 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Programmatically scroll a UIScrollView

#1
I have a `UIScrollView` which has several views. When a user flicks their finger, the view scrolls to the right or left depending on the direction of the finger flick. Basically my code works in a way similar to the iPhone photo app. Now, is there a way that I can programmatically do the same thing so that I end up with a slideshow that runs on its own with a click of a button and a configurable pause between each scroll?

How do you really do slideshows with `UIScrollView`?
Reply

#2
Another way is

scrollView.contentOffset = CGPointMake(x,y);
Reply

#3
If you want control over the duration and style of the animation, you can do:

[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
scrollView.contentOffset = CGPointMake(x, y);
} completion:NULL];

Adjust the duration (`2.0f`) and options (`UIViewAnimationOptionCurveLinear`) to taste!
Reply

#4
- (void)viewDidLoad
{
[super viewDidLoad];
board=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, 80)];
board.backgroundColor=[UIColor greenColor];
[self.view addSubview:board];
// Do any additional setup after loading the view.
}


-(void)viewDidLayoutSubviews
{


NSString *str=@"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

index=1;
for (int i=0; i<20; i++)
{
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(-50, 15, 50, 50)];
lbl.tag=i+1;
lbl.text=[NSString stringWithFormat:@"%c",[str characterAtIndex:arc4random()%str.length]];
lbl.textColor=[UIColor darkGrayColor];
lbl.textAlignment=NSTextAlignmentCenter;
lbl.font=[UIFont systemFontOfSize:40];
lbl.layer.borderWidth=1;
lbl.layer.borderColor=[UIColor blackColor].CGColor;
[board addSubview:lbl];
}

[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(CallAnimation) userInfo:nil repeats:YES];

NSLog(@"%d",[board subviews].count);
}


-(void)CallAnimation
{

if (index>20) {
index=1;
}
UIView *aView=[board viewWithTag:index];
[self doAnimation:aView];
index++;
NSLog(@"%d",index);
}

-(void)doAnimation:(UIView*)aView
{
[UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
aView.frame=CGRectMake(self.view.frame.size.height, 15, 50, 50);
}
completion:^(BOOL isDone)
{
if (isDone) {
//do Somthing
aView.frame=CGRectMake(-50, 15, 50, 50);
}
}];
}

Reply

#5
With Animation in Swift

scrollView.setContentOffset(CGPointMake(x, y), animated: true)
Reply

#6
scrollView.setContentOffset(CGPoint(x: y, y: x), animated: true)
Reply

#7
[Scrollview setContentOffset:CGPointMake(x, y) animated:YES];
Reply

#8
You can scroll to some point in a scroll view with one of the following statements in Objective-C


[scrollView setContentOffset:CGPointMake(x, y) animated:YES];

or Swift

scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)

See the [guide "*Scrolling the Scroll View Content*" from Apple as well](

[To see links please register here]

).

To do slideshows with `UIScrollView`, you arrange all images in the scroll view, set up a repeated timer, then `-setContentOffset:animated:` when the timer fires.

But a more efficient approach is to use 2 image views and swap them using transitions or simply switching places when the timer fires. See

[To see links please register here]

for details.
Reply

#9
I'm amazed that this topic is 9 years old and the actual straightforward answer is not here!

What you're looking for is `scrollRectToVisible(_:animated:)`.

Example:

```swift
extension SignUpView: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
scrollView.scrollRectToVisible(textField.frame, animated: true)
}
}
```

What it does is exactly what you need, and it's far better than hacky `contentOffset`

> This method scrolls the content view so that the area defined by rect
> is just visible inside the scroll view. If the area is already
> visible, the method does nothing.

From:

[To see links please register here]

Reply

#10
Swift 3

let point = CGPoint(x: 0, y: 200) // 200 or any value you like.
scrollView.contentOffset = point
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through