scrollview的delegate与files owner关联
.h文件
#import <UIKit/UIKit.h>
@interface EXSVViewController : UIViewController<UIScrollViewDelegate>
@end
.m文件
//
// EXSVViewController.m
// ExerciseScrollView
//
// Created by hxl on 13-5-21.
// Copyright (c) 2013年 xiaolei.hu. All rights reserved.
//
#import "EXSVViewController.h"
@interface EXSVViewController ()
@property (nonatomic) IBOutlet UIScrollView *scrollView;//在xib中与scrollview控件关联
@end
@implementation EXSVViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Scroll view
UInt16 h = 6;
UInt16 w = 6;
for (UInt16 i = 0; i < h; i++) {
//CGRectMake(X轴坐标,Y轴坐标,宽,高)
for (UInt16 j = 0; j < w; j++) {
//设置图片位置
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, self.scrollView.frame.size.height * j, self.scrollView.frame.size.width,self.scrollView.frame.size.height)];
// 指定URL生成UIImage
[button setTitle:@"按钮" forState:UIControlStateNormal]; //设置button的标题
//[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside]; //定义点击时的响应函数
[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]] forState:UIControlStateNormal];
[self.scrollView addSubview:button];
}
}
//NSLog(@"%@",self);
//等同于在.xlb文件中delegate和File's Owner连接
//[self.scrollView setDelegate:self];
[self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width * h, self.scrollView.frame.size.height * w)];
//[self.scrollView setDirectionalLockEnabled:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
typedef enum {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);//系统默认不支持旋转功能
//返回YES屏幕所有方向旋转都支持
//return YES;
}
// 触摸屏幕来滚动画面还是其他的方法使得画面滚动,皆触发该函数
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"Scrolling...");
}
// 触摸屏幕并拖拽画面,再松开,最后停止时,触发该函数
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"scrollViewDidEndDragging - End of Scrolling.");
}
// 滚动停止时,触发该函数
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"scrollViewDidEndDecelerating - End of Scrolling.");
}
// 调用以下函数,来自动滚动到想要的位置,此过程中设置有动画效果,停止时,触发该函数
// UIScrollView的setContentOffset:animated:
// UIScrollView的scrollRectToVisible:animated:
// UITableView的scrollToRowAtIndexPath:atScrollPosition:animated:
// UITableView的selectRowAtIndexPath:animated:scrollPosition:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
NSLog(@"scrollViewDidEndScrollingAnimation - End of Scrolling.");
}
@end