溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

UIPageControl圖片下面的白點(diǎn),并且和圖片同步

發(fā)布時(shí)間:2020-07-03 17:08:07 來(lái)源:網(wǎng)絡(luò) 閱讀:781 作者:Im劉亞芳 欄目:開(kāi)發(fā)技術(shù)

類(lèi)和文件

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;  //代理協(xié)議方法
    [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)];
     //設(shè)置白點(diǎn)數(shù)量
    pageControl .tag = 1000;
    pageControl.numberOfPages = 15;
    pageControl.pageIndicatorTintColor = [UIColor brownColor];//沒(méi)有選中的顏色
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];//已經(jīng)被選中的顏色   。。。。
    pageControl.backgroundColor = [UIColor blueColor];
    pageControl.alpha = 0.3;
    //當(dāng)值改變的時(shí)候,調(diào)用綁定的方法
    [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滾動(dòng),就調(diào)用這個(gè)方法
    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);
    //計(jì)算當(dāng)前是第幾頁(yè)
    int page = scrollView.contentOffset.x / scrollView.frame.size.width;
    NSLog(@"%d",page);
    
    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:1000];
   
    //在換不同頁(yè)面是,原來(lái)的頁(yè)面回到初始位置
    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(@"開(kāi)始減速");
}
- (void)pageAction:(UIPageControl *)pageControl
{
    //pageControl顯示的當(dāng)前頁(yè)數(shù)(從0開(kāi)始 )
    NSLog(@"第%d頁(yè)",pageControl.currentPage);
    // 通過(guò)調(diào)整scrollView的偏移量,讓scrollView調(diào)整位置
//    _scrollView.contentOffset = CGPointMake(280 * pageControl.currentPage, 0);
    [_scrollView setContentOffset:CGPointMake(280 * pageControl.currentPage, 0) animated:YES];
    
    NSLog(@"翻頁(yè)");
}
- (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



向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI