#import "ViewController.h"
@interface ViewController ()
/** 一个scrollView 控件 */
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end
@implementation ViewController
/** 点击最上面按钮回到最上面*/
/**
contentOffset: 设置偏移量
根据自己要求设置偏移量.
*/
- (IBAction)top:(id)sender {
CGPoint offset = CGPointMake(self.scrollView.contentOffset.x, 0);
[self.scrollView setContentOffset:offset animated:YES];
}
- (IBAction)left:(id)sender {
// 动画用法1
[UIView animateWithDuration:2.0 animations:^{
self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentOffset.y);
}];
}
- (IBAction)right:(id)sender {
// 动画用法2 麻烦,不建议用
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:2.0];
[UIView setAnimationDelegate:self]; // 可设置代理
[UIView setAnimationDidStopSelector:@selector(stop)];
[UIView setAnimationWillStartSelector:@selector(start)];
CGFloat offsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width;
self.scrollView.contentOffset = CGPointMake(offsetX, self.scrollView.contentOffset.y);
[UIView commitAnimations]; // 结束动画
}
- (IBAction)bottom:(id)sender {
// CGFloat offsetY = self.scrollView.contentSize.height - self.scrollView.frame.size.height;
// CGPoint offset = self.scrollView.contentOffset;
// offset.y = offsetY;
// self.scrollView.contentOffset = offset;
// 动画用法3
[UIView animateWithDuration:2.0 animations:^{
CGFloat offsetY = self.scrollView.contentSize.height - self.scrollView.frame.size.height;
self.scrollView.contentOffset = CGPointMake(0, offsetY);
} completion:^(BOOL finished) {
NSLog(@"完成");
}];
// self.scrollView.contentOffset.y = offsetY;
// OC语法细节:不允许直接修改OC对象的结构体属性的成员
}