Programowo przewijaj UIScrollView
Mam UIScrollView
, który ma kilka odsłon. Gdy użytkownik przesunie palcem, widok przewija się w prawo lub w lewo, w zależności od kierunku ruchu palca. Zasadniczo mój kod działa w sposób podobny do iPhone photo app. Czy istnieje sposób, że mogę programowo zrobić to samo, aby skończyć z pokazem slajdów, który działa samodzielnie za pomocą kliknięcia przycisku i konfigurowalnej pauzy między każdym przewijaniem?
Jak naprawdę zrobić pokazy slajdów z UIScrollView
?
9 answers
Możesz przewijać do pewnego punktu w widoku przewijania za pomocą jednego z następujących poleceń w Objective-C
[scrollView setContentOffset:CGPointMake(x, y) animated:YES];
Lub Swift
scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)
Zobacz przewodnik "przewijanie zawartości widoku przewijania " firmy Apple również .
Aby wykonywać pokazy slajdów z UIScrollView
, układasz wszystkie obrazy w widoku przewijania, ustawiasz powtarzany timer, a następnie -setContentOffset:animated:
gdy timer zostanie wywołany.
Ale bardziej efektywnym podejściem jest użycie 2 widoków obrazów i zamiana ich za pomocą przejść lub po prostu przełączanie miejsc po uruchomieniu timera. Zobacz pokaz slajdów iPhone ' a Po szczegóły.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-30 11:27:33
Jeśli chcesz kontrolować czas trwania i styl animacji, możesz to zrobić:
[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
scrollView.contentOffset = CGPointMake(x, y);
} completion:NULL];
Dostosuj czas trwania (2.0f
) i opcje (UIViewAnimationOptionCurveLinear
) do smaku!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-03 04:49:09
Innym sposobem jest
scrollView.contentOffset = CGPointMake(x,y);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-04 19:36:56
Z animacją w języku Swift
scrollView.setContentOffset(CGPointMake(x, y), animated: true)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-17 17:39:46
Swift 3
let point = CGPoint(x: 0, y: 200) // 200 or any value you like.
scrollView.contentOffset = point
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-15 18:29:09
scrollView.setContentOffset(CGPoint(x: y, y: x), animated: true)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-17 03:47:55
[Scrollview setContentOffset:CGPointMake(x, y) animated:YES];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-06 12:09:29
- (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);
}
}];
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-12 11:46:00
Pokazuję, jak stworzyć prosty widok przewijania za pomocą dwóch UIView i dodać pewne ograniczenia układu automatycznego, aby przewijał się płynnie.
Zakładam, że masz pusty kontroler widoku w main.storyboard.
1.Dodaj UIView .
let view = UIView()
view.backgroundColor = UIColor.green
self.view.addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
top = view.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0)
left = view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0)
right = view.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0)
bottom = view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0)
height = view.heightAnchor.constraint(equalToConstant: self.view.frame.height)
NSLayoutConstraint.activate([top, right, left, height, bottom])
2.Dodaj ScrollView.
let scroll = UIScrollView()
scroll.backgroundColor = UIColor.red
view.addSubview(scroll)
scroll.translatesAutoresizingMaskIntoConstraints = false
top = scroll.topAnchor.constraint(equalTo: view.topAnchor, constant: 10)
left = scroll.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0)
right = scroll.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0)
bottom = scroll.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10)
NSLayoutConstraint.activate([top, right, left, bottom])
3.Dodaj pierwszy UILabel .
let l2 = UILabel()
l2.text = "Label1"
l2.textColor = UIColor.black
l2.backgroundColor = UIColor.blue
scroll.addSubview(l2)
l2.translatesAutoresizingMaskIntoConstraints = false
top = l2.topAnchor.constraint(equalTo: scroll.topAnchor, constant: 10)
left = l2.leadingAnchor.constraint(equalTo: scroll.leadingAnchor, constant: 10)
height = l2.heightAnchor.constraint(equalToConstant: 100)
right = l2.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10)
NSLayoutConstraint.activate([top, left, right, height])
4.Dodaj Drugi UILabel .
let l1 = UILabel()
l1.text = "Label2"
l1.textColor = UIColor.black
l1.backgroundColor = UIColor.blue
scroll.addSubview(l1)
l1.translatesAutoresizingMaskIntoConstraints = false
top = l1.topAnchor.constraint(equalTo: scroll.topAnchor, constant: 1000)
left = l1.leadingAnchor.constraint(equalTo: scroll.leadingAnchor, constant: 10)
bottom = l1.bottomAnchor.constraint(equalTo: scroll.bottomAnchor, constant: -10)
height = l1.heightAnchor.constraint(equalToConstant: 100)
right = l1.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10)
NSLayoutConstraint.activate([left, bottom, right, height, top])
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-03 09:10:21