1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| #import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>//遵守协议
{
UIScrollView *_scrollView; }
@property (weak, nonatomic) IBOutlet UIButton *myButton; @end
@implementation ViewController
- (IBAction)onClick:(UIButton *)sender { [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES]; }
- (void)viewDidLoad { [super viewDidLoad]; [self createScrollView]; [self addSubView]; [self customScrollView]; [self.view bringSubviewToFront:self.myButton];
}
-(void)createScrollView{
_scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
_scrollView.backgroundColor = [UIColor orangeColor];
_scrollView.delegate = self; [self.view addSubview:_scrollView]; } -(void)addSubView{
NSString *path = [[NSBundle mainBundle]pathForResource:@"海贼02" ofType:@"jpg"];
NSData *data = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)]; imageView.image = image;
_scrollView.contentSize = CGSizeMake(image.size.width, image.size.height); [_scrollView addSubview:imageView]; }
-(void)customScrollView{
#pragma mark 滚动相关
_scrollView.bounces = YES;
_scrollView.alwaysBounceHorizontal = YES;
_scrollView.alwaysBounceVertical = YES;
_scrollView.scrollEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = YES; _scrollView.showsVerticalScrollIndicator = YES;
_scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
_scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 30, 60); _scrollView.scrollsToTop = YES; } #pragma mark scrollView delegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ NSLog(@"将要拖拽"); }
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
} -(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{ NSLog(@"将要结束拖拽"); }
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"拖拽结束");
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{ NSLog(@"将要减速"); }
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ NSLog(@"减速结束");
} @end
|