您的位置:首页 > 产品设计 > UI/UE

UI - UIScrollView

2015-09-24 18:09 495 查看
<AppDelegate.m>

#import "AppDelegate.h"
#import "RootViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

RootViewController *rootVC = [[RootViewController alloc ]init ];
self.window.rootViewController = rootVC;
[rootVC release];

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


<RootViewController.h>

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end


<RootViewController.m>

#import "RootViewController.h"

@interface RootViewController ()<UIScrollViewDelegate>
@property (nonatomic,retain)UIScrollView *scrollview;
@property (nonatomic,retain)UIImageView *imageView;
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

//布局 scrollview
[self layoutScrollView];
//布局 imageView
[self layoutImageView];
}

/*
UIScrolView 是 UI 中可滑动的视图控件,他扩充了 UIView 滑动的功能,他的核心功能有两个:滑动和缩放
我们今后学习的 UITableView 和 UITextView 等可以滑动的控件都是继承与 UIScrollView
作为 UI 中的控件本质和 UITale UIButton 等没有差别  所以使用过程也是分四步
*/

-(void)layoutScrollView
{
//创建对象
self.scrollview = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//配置属性
//(1)设置背景颜色
_scrollview.backgroundColor = [UIColor purpleColor];
//(2)设置显示内容的大小
_scrollview.contentSize = CGSizeMake(320 * 2, 568 * 2);
//(3)设置滚动条的风格
_scrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动条颜色默认为黑色的
//(4)设置整屏滑动
//    _scrollview.pagingEnabled = YES;
//(5)设置图片的偏移
_scrollview.contentOffset = CGPointMake(320, 568);
//与(5)效果相同
[_scrollview scrollRectToVisible:CGRectMake(0, 0, 320, 568) animated:YES];
//(6)设置点击状态栏回到顶部   默认是开启的
_scrollview.scrollsToTop = YES;
//(7)设置回弹效果     默认的是开启回弹效果
_scrollview.bounces = NO;
//(8)设置滚动条状态      默认水平和竖直滚动条都是显示的
//    _scrollview.showsHorizontalScrollIndicator = NO;  //水平方向滚动条不显示
//    _scrollview.showsVerticalScrollIndicator = NO;    //水平方向滚动条不显示
//(9)设置代理
_scrollview.delegate = self;

//添加至父视图
[self.view addSubview:_scrollview];
//释放所有权
[_scrollview release];

//缩放    ,在有多个 scrollview 中,设置哪个 scrollview 的缩放属性 才会缩放它

//设置视图能得放大的最大值
_scrollview.maximumZoomScale = 2;
//设置视图能够缩小的最小值
_scrollview.minimumZoomScale = 0.1;
//设置尺度比例    默认为1
_scrollview.zoomScale = 1;
//设置缩放回弹   默认为 YES
_scrollview.bouncesZoom = NO;
//确定在用户解除他们的手指后的减速率
_scrollview.decelerationRate = 0.1;
//控制控件是否接触取消touch的事件,默认 yes, 如果为 no, 一旦我们开始跟踪,则不可以拖拽触摸
_scrollview.canCancelContentTouches = YES;
//是否在拖拽,只读
_scrollview.dragging;
//是否在跟踪,只读
_scrollview.tracking;
//是否在减速,只读
_scrollview.decelerating;
//决定是否推迟滚动视图触摸手势的处理,默认yes,使用上面的150ms的timer,如果设置为NO,touch事件立即传递给subView,不会有150ms的等待。
_scrollview.delaysContentTouches = YES;
//默认为YES,如果设置为NO,这消息一旦传递给subView,这scroll事件不会再发生。
_scrollview.canCancelContentTouches = YES;
}
-(void)layoutImageView
{
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"夕阳.jpg" ]];
_imageView.frame = CGRectMake(0, 0, 320 * 2, 568 * 2);
[_scrollview addSubview:_imageView];
[_imageView release];
}

//===========================scrollview 协议方法=======================

//滑动时触发的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
NSLog(@"正在滑动 %s  %d ",__FUNCTION__,__LINE__);
}
//将要被拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"将要拖拽 %s  %d ",__FUNCTION__,__LINE__);
}
//将要结束拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
NSLog(@"将要结束拖拽  %s   %d",__FUNCTION__,__LINE__);
}
//已经结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"已经结束拖拽  %s   %d",__FUNCTION__,__LINE__);
}
//将要开始减速     结束拖拽后立即执行此方法
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
NSLog(@"将要开始减速  %s   %d",__FUNCTION__,__LINE__);
}
//已经结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"已经结束减速  %s   %d",__FUNCTION__,__LINE__);
}

//缩放时触发的方法
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
NSLog(@"正在缩放  %s    %d",__FUNCTION__,__LINE__);
}
//将要开始缩放
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
NSLog(@"将要开始缩放  %s   %d",__FUNCTION__,__LINE__);
}
//已经完成缩放
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
NSLog(@"已经完成缩放  %s   %d",__FUNCTION__,__LINE__);
}
//指定缩放的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
//指定 imageView 可以被缩放
return _imageView ;
}
//是否可以回到到顶部, 默认 YES
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
return YES;
}
//已经回到顶部
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
NSLog(@"已经回到顶部  %s   %d",__FUNCTION__,__LINE__);
}
//已经结束滑动,  设置 setContentOffset/scrollRectVisible:animated: 且 animation 为 YES 后,才会触发
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
NSLog(@"已经结束滑动  %s   %d",__FUNCTION__,__LINE__);
}

@end


附图一张属性总结:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: