您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
类和文件
AppDelegate.m
#import "AppDelegate.h" #import "MainViewController.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]; MainViewController *mainVC = [[MainViewController alloc] init]; self.window.rootViewController = mainVC; [mainVC release]; [_window release]; return YES; } - (void)dealloc { [_window release]; [super dealloc]; } - (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
MainViewController.h
#import <UIKit/UIKit.h> @interface MainViewController : UIViewController @property (nonatomic , retain)UIScrollView *scrollView; @end
MainViewController.m
#import "MainViewController.h" @interface MainViewController ()<UIScrollViewAccessibilityDelegate> @end @implementation MainViewController //@synthesize scrollView; - (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. self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 280, 400)]; self.scrollView.contentSize = CGSizeMake(280*15, 0); self.scrollView.pagingEnabled = YES; self.scrollView.delegate = self; //代理协议方法 [self.view addSubview:_scrollView]; [_scrollView release]; for (int i = 0; i < 15; i++) { UIImageView *p_w_picpathView = [[UIImageView alloc] initWithFrame:CGRectMake(0*i, 0, 280, 400)]; UIScrollView *scrollView1 = [[UIScrollView alloc] initWithFrame:CGRectMake(280*(i), 0, 280, 480)]; scrollView1.delegate = self; scrollView1.maximumZoomScale = 2.0; scrollView1.minimumZoomScale = 0.5; [self.scrollView addSubview:scrollView1]; [scrollView1 release]; NSString *name = [NSString stringWithFormat:@"a%d.jpg",i]; p_w_picpathView.p_w_picpath = [UIImage p_w_picpathNamed:name]; [scrollView1 addSubview:p_w_picpathView]; [p_w_picpathView release]; } UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(20, 420, 280, 20)]; //设置白点数量 pageControl .tag = 1000; pageControl.numberOfPages = 15; pageControl.pageIndicatorTintColor = [UIColor brownColor];//没有选中的颜色 pageControl.currentPageIndicatorTintColor = [UIColor redColor];//已经被选中的颜色 。。。。 pageControl.backgroundColor = [UIColor blueColor]; pageControl.alpha = 0.3; //当值改变的时候,调用绑定的方法 [pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:pageControl]; [pageControl release]; } - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ return [scrollView.subviews firstObject]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { //只要scrollView滚动,就调用这个方法 if (scrollView.contentOffset.x > 3940) { scrollView.contentOffset = CGPointMake(0, 0); // [scrollView setContentOffset:CGPointMake(-50, 0) animated:YES]; } else if (scrollView.contentOffset.x < -30) { scrollView.contentOffset = CGPointMake(3940, 0); } NSLog(@"偏移量%f",scrollView.contentOffset.x); //计算当前是第几页 int page = scrollView.contentOffset.x / scrollView.frame.size.width; NSLog(@"%d",page); UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:1000]; //在换不同页面是,原来的页面回到初始位置 int a = pageControl.currentPage; pageControl.currentPage = page; if (a != page) { for (int i = 0; i < 15; i++) { UIScrollView *p = [self.scrollView.subviews objectAtIndex:i]; p.zoomScale = 1.0; } } } - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView { NSLog(@"开始减速"); } - (void)pageAction:(UIPageControl *)pageControl { //pageControl显示的当前页数(从0开始 ) NSLog(@"第%d页",pageControl.currentPage); // 通过调整scrollView的偏移量,让scrollView调整位置 // _scrollView.contentOffset = CGPointMake(280 * pageControl.currentPage, 0); [_scrollView setContentOffset:CGPointMake(280 * pageControl.currentPage, 0) animated:YES]; NSLog(@"翻页"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。