Objective C iOS简单图像旋转类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Objective C iOS简单图像旋转类相关的知识,希望对你有一定的参考价值。
// // Carousel.h // #import <UIKit/UIKit.h> @interface Carousel : UIView <UIScrollViewDelegate> { UIPageControl *pageControl; NSArray *images; } @property (nonatomic, retain) UIPageControl *pageControl; - (void)setup; @end // // Carousel.m // #import "Carousel.h" @implementation Carousel @synthesize pageControl; @synthesize images; #pragma mark - Override images setter { if (newImages != images) { [newImages retain]; [images release]; images = newImages; [self setup]; } } #pragma mark - Carousel setup - (void)setup { UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.frame]; [scrollView setDelegate:self]; [scrollView setShowsHorizontalScrollIndicator:NO]; [scrollView setPagingEnabled:YES]; [scrollView setBounces:NO]; CGSize scrollViewSize = scrollView.frame.size; for (NSInteger i = 0; i < [self.images count]; i++) { CGRect slideRect = CGRectMake(scrollViewSize.width * i, 0, scrollViewSize.width, scrollViewSize.height); UIView *slide = [[UIView alloc] initWithFrame:slideRect]; [slide setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.frame]; [imageView setImage:[UIImage imageNamed:[self.images objectAtIndex:i]]]; [slide addSubview:imageView]; [imageView release]; [scrollView addSubview:slide]; [slide release]; } UIPageControl *tempPageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, scrollViewSize.height - 20, scrollViewSize.width, 20)]; [self setPageControl:tempPageControll]; [tempPageControll release]; [self.pageControl setNumberOfPages:[self.images count]]; [scrollView setContentSize:CGSizeMake(scrollViewSize.width * [self.images count], scrollViewSize.height)]; [self addSubview:scrollView]; [scrollView release]; [self addSubview:self.pageControl]; } #pragma mark - UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat pageWidth = scrollView.frame.size.width; [self.pageControl setCurrentPage:page]; } #pragma mark - Cleanup - (void)dealloc { [pageControl release]; [images release]; [super dealloc]; } @end // // ViewController.m // SimpleCarousel // #import "ViewController.h" #import "Carousel.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Initialize carousel object with frame size of images Carousel *carousel = [[Carousel alloc] initWithFrame:CGRectMake(0, 0, 320, 205)]; // Add some images to carousel, we are passing autoreleased NSArray // Add carousel to view [self.view addSubview:carousel]; // Cleanup [carousel release]; } @end
以上是关于Objective C iOS简单图像旋转类的主要内容,如果未能解决你的问题,请参考以下文章
当我超出父图像的范围时,iOS/Objective C 图像 alpha 低
tableview 图像未正确显示 - iOS 8 iPhone App Objective C
将 iPhone X 旋转为横向时,封面图像的左侧和下方出现空白