I try to add Image into my view that in UIScrollView
here is my .h
#interface NormalDetail : UIViewController <UIScrollViewDelegate> {
IBOutlet UIImageView *lineImg;
}
#property (nonatomic, retain) UIImageView *lineImg;
#end
and here is my .m
- (void)viewDidLoad
{
[super viewDidLoad];
//Set ScrollView
scrollView.contentSize = CGSizeMake(320.0f, 1800.0f);
//End Set ScrollView
[self setupTextView];
}
- (void) setupTextView
{
textDescription.scrollEnabled = NO;
NSString *foo = #"Hello Test";
textDescription.text = foo;
/*Here is something for dynamic textview*/
CGRect frame;
frame = textDescription.frame;
frame.size.height = [textDescription contentSize].height;
textDescription.frame = frame;
/**/
int linePosition = frame.size.height+20;
lineImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"line.png"]];
CGRect line2Frame = CGRectMake(-8, 600, 328, 10);
lineImg.frame = line2Frame;
}
What's wrong with my code , My pic didn't appear. help me please ;(
You will needed to the UIImageView to the scorllView.
- (void) setupTextView
{
textDescription.scrollEnabled = NO;
NSString *foo = #"Hello Test";
textDescription.text = foo;
/*Here is something for dynamic textview*/
CGRect frame;
frame = textDescription.frame;
frame.size.height = [textDescription contentSize].height;
textDescription.frame = frame;
/**/
int linePosition = frame.size.height+20;
lineImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"line.png"]];
CGRect line2Frame = CGRectMake(-8, 600, 328, 10);
lineImg.frame = line2Frame;
//Add the image view as a subview to yout scrollview.
[scrollView addSubView:lineImg];
}
Related
Hello everyone i need to add zoom in uiscrollview containing several uiimageviews. Here is my code how I am doing it but its not working properly
- (void)setupPage
{
scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSUInteger nimages = 0;
CGFloat cx = 0;
for (; ; nimages++) {
//Maquette final_Print 2.png
NSString *imageName = [NSString stringWithFormat:#"rint %d.png", (nimages + 1)];
UIImage *image = [UIImage imageNamed:imageName];
if (image == nil) {
break;
}
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setContentMode:UIViewContentModeScaleToFill];
CGRect rect = imageView.frame;
rect.size.height = 1004;
rect.size.width = 768;
rect.origin.x = ((scrollView.frame.size.width - 768) / 2) + cx;
rect.origin.y = ((scrollView.frame.size.height - 1004) / 2);
imageView.frame = rect;
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
[scrollView setContentSize:CGSizeMake(cx, 1004)];
scrollView.minimumZoomScale = scrollView.frame.size.width / 768;
scrollView.maximumZoomScale = 2.0;
[scrollView setZoomScale:scrollView.minimumZoomScale];
}
// Code for handling zooming :
#pragma mark -
#pragma mark UIScrollView ZOOM
//- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
// imageView.frame = [self centeredFrameForScrollView:scrollView andUIView:imageView];;
//}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
- (CGRect)centeredFrameForScrollView:(UIScrollView *)scroll andUIView:(UIView *)rView {
CGSize boundsSize = scroll.bounds.size;
CGRect frameToCenter = rView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width) {
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
}
else {
frameToCenter.origin.x = 0;
}
// center vertically
if (frameToCenter.size.height < boundsSize.height) {
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
}
else {
frameToCenter.origin.y = 0;
}
return frameToCenter;
}
When I am trying to zoom its not working as expected. And also i cannot swipe left or right...
.m File
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.theImageView;
}
- (CGRect)centeredFrameForScrollView:(UIScrollView *)scroll andUIView:(UIView *)rView {
CGSize boundsSize = scroll.bounds.size;
CGRect frameToCenter = rView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width) {
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
}
else {
frameToCenter.origin.x = 0;
}
// center vertically
if (frameToCenter.size.height < boundsSize.height) {
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
}
else {
frameToCenter.origin.y = 0;
}
return frameToCenter;
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
self.theImageView.frame = [self centeredFrameForScrollView:self.theScrollView andUIView:self.theImageView];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.resourceData setLength:0];
self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.resourceData appendData:data];
NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.resourceData length]];
self.progressBar.progress = [resourceLength floatValue] / [self.filesize floatValue];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.theImage = [[UIImage alloc]initWithData:resourceData];
self.theImageView.frame = CGRectMake(0, 0, self.theImage.size.width, self.theImage.size.height);
self.theImageView.image = self.theImage;
self.theScrollView.minimumZoomScale = self.theScrollView.frame.size.width / self.theImageView.frame.size.width;
self.theScrollView.maximumZoomScale = 2.0;
[self.theScrollView setZoomScale:self.theScrollView.minimumZoomScale];
self.theScrollView.contentSize = self.theImageView.frame.size;
self.theLabel.hidden = YES;
self.progressBar.hidden = YES;
}
-(void)setImageInImageView
{
NSURLRequest *req = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.imageLink]];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:req delegate:self];
if (conn)
{
self.resourceData = [NSMutableData data];
}
else
{
NSLog(#"Connection failed: IMageViewerViewController");
}
}
-(void)loadView
{
self.filesize = [[NSNumber alloc]init];
self.progressBar = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
self.progressBar.frame = CGRectMake(20, 240, 280, 40);
[self.progressBar setProgress:0.0];
self.theImageView = [[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]]autorelease];
self.theScrollView = [[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]]autorelease];
self.theScrollView.delegate = self;
[self.theScrollView addSubview:self.theImageView];
self.view = self.theScrollView;
self.theLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, 320, 40)];
self.theLabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.theLabel.text = #"Please wait, file is being downloaded";
self.theLabel.textAlignment = UITextAlignmentCenter;
self.theLabel.hidden = NO;
[self.view addSubview:self.progressBar];
[self.view bringSubviewToFront:self.progressBar];
[self.view addSubview:self.theLabel];
[self.view bringSubviewToFront:self. theLabel];
[self performSelectorOnMainThread:#selector(setImageInImageView) withObject:nil waitUntilDone:NO];
}
.h File
#interface ImageViewerViewController : UIViewController<UIScrollViewDelegate, NSURLConnectionDelegate, NSURLConnectionDataDelegate>
#property (nonatomic, retain) IBOutlet UIImageView *theImageView;
#property (nonatomic, retain) IBOutlet UIScrollView *theScrollView;
#property (nonatomic, retain) NSString *imageLink;
#property (nonatomic, retain) UIImage *theImage;
#property (nonatomic, retain) UILabel *theLabel;
#property (nonatomic, retain) UIProgressView *progressBar;
#property (nonatomic, retain) NSMutableData *resourceData;
#property (nonatomic, retain) NSNumber *filesize;
#end
All you need to do is add your UIImageView (or any view you want to zoom) inside your UIScrollView.
Set your maxZoomScale on your UIScrollView to any value higher than 1.0f.
Set yourself as the delegate of your UIScrollView and return the UIImageView in the viewForZooming delegate method.
That's it. No pinch gesture needed, no nothing. UIScrollView handles pinch zooming for you.
I have a UIView that contains drop shadows and corners which I am loading four of in my UIViewController and I am seeing a performance hit when the screen loads. Since I am using the same white background with shadows and corner radii I figured I would store the UIView in NSCache.
When I run the app there is a large gap where the first UIView should be, however, it is not showing up. What does show up is the last view in my list of views. If I comment out the last one and run it again, the third one shows up. It seems like I am having an issue with the pointer in memory but not sure. Perhaps I am using NSCache incorrectly?
(Note: The first view shown is not using the NSCache)
Here is how I am using the NSCache:
.h file
#interface LunchDetailViewController : UIViewController <UIScrollViewDelegate>
#property (nonatomic) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) NSCache *entreeViewsCache;
#end
.m file
#synthesize scrollView;
#synthesize entreeViewsCache;
- (void)viewDidLoad
{
[super viewDidLoad];
self.entreeViewsCache = [[NSCache alloc] init];
UIView *entreeView = [[UIView alloc] init];
entreeView.backgroundColor = [UIColor whiteColor];
entreeView.layer.masksToBounds = NO;
entreeView.layer.cornerRadius = 3.0;
entreeView.layer.shadowOffset = CGSizeMake(1.1, 2.1);
entreeView.layer.shadowOpacity = 0.2;
[self.entreeViewsCache setObject:entreeView forKey:#"EntreeView"];
}
- (void) configureScrollView
{
// This line of code allows the scroll view to be 'scrollable'.
self.scrollView.contentSize = CGSizeMake(320, 620);
UIView *elementaryRoundedCornerView = [self.entreeViewsCache objectForKey:#"EntreeView"];
elementaryRoundedCornerView.frame = CGRectMake(15,15,290,180);
UIView *middleRoundedCornerView = [self.entreeViewsCache objectForKey:#"EntreeView"];
middleRoundedCornerView.frame = CGRectMake(15,210,290,180);
UIView *highRoundedCornerView = [self.entreeViewsCache objectForKey:#"EntreeView"];
highRoundedCornerView.frame = CGRectMake(15,404,290,180);
NSMutableArray *entreeItems = [[NSMutableArray alloc] initWithObjects:#"Pancakes w/ Sausage Patties", #"Corn Dog", #"Grilled Cheese Sandwhich", #"Chicken Tender Wraps", nil];
UIView *elementaryLunchMenuDetails = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 240, 160)];
[elementaryLunchMenuDetails addSubview:[self returnNativeCode:entreeItems rectDimensions:CGRectMake(2, 5, 215, 160) schoolType:#"Elementary"]];
[elementaryRoundedCornerView addSubview:elementaryLunchMenuDetails];
UIView *middleLunchMenuDetails = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 240, 160)];
[middleLunchMenuDetails addSubview:[self returnNativeCode:entreeItems rectDimensions:CGRectMake(2, 2, 215, 160) schoolType:#"Middle"]];
[middleRoundedCornerView addSubview:middleLunchMenuDetails];
UIView *highLunchMenuDetails = [[UIView alloc] initWithFrame:CGRectMake(10,10, 240, 160)];
[highLunchMenuDetails addSubview:[self returnNativeCode:entreeItems rectDimensions:CGRectMake(2, 2, 215, 160) schoolType:#"High"]];
[highRoundedCornerView addSubview:highLunchMenuDetails];
[self.scrollView addSubview:elementaryRoundedCornerView];
[self.scrollView addSubview:middleRoundedCornerView];
[self.scrollView addSubview:highRoundedCornerView];
}
Wow. That's clever. But not correct.
Instead of using NSCache to duplicate a view, you probably want to create a UIView subclass that formats the view the way you want. Then just throw a bunch of those views on your scrollview.
ABCView.m
#implementation ABCDayView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.layer.masksToBounds = NO;
self.layer.cornerRadius = 3.0;
self.layer.shadowOffset = CGSizeMake(1.1f, 2.1f);
self.layer.shadowOpacity = 0.2f;
}
return self;
}
- (void)setItems:(NSArray *)items
{
if ([_items isEqualToArray:items] == NO) {
_items = items;
[self createItemViews];
[self setNeedsLayout];
}
}
// You'll also need to add -createItemViews and -setNeedsLayout methods.
.m file
- (void)configureScrollView
{
NSMutableArray *entreeItems = #[#"Pancakes w/Sausage Patties",
#"Corn Dog",
#"Grilled Cheese Sandwhich",
#"Chicken Tender Wraps"];
CGRect frame = CGRectMake(15,15,290,180);
ABCDayView *elementaryView = [[ABCDayView alloc] initWithFrame:frame];
elementaryView.items = entreeItems;
CGFloat y = CGRectGetMaxY(elementaryView.frame) + 10.0f;
frame = CGRectMake(15, y, 290, 180);
ABCDayView *middleView = [[ABCDayView alloc] initWithFrame:frame];
middleView.items = entreeItems;
...
CGFloat length = // Use CGRectGetMaxY on the last frame to get the length.
self.scrollView.contentSize = CGSizeMake(320, length);
}
That's by no means perfect code. But hopefully it will give you an idea of a better way to implement this.
I am trying to make a survey app but I can't make the memory management although I am using ARC. My app makes memory leak. I get memory warning. When I make my app in interface builder statically there is no problem but when i make my app programmatically I get memory problem. Why is that happening? Here is my code:
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
int soruAdedi = 20;
#interface ViewController ()
#end
#implementation ViewController
#synthesize scrollView2;
-(void)textViewBicimle: (UITextField *)textAlani{
[textAlani.layer setBorderColor:[[[UIColor blackColor] colorWithAlphaComponent:1.0] CGColor]];
[textAlani.layer setBorderWidth:1.0];
}
- (void) tableCiz:(NSMutableArray *)hucreler sutun:(int)sutunSayisi satir:(int)satirSayisi{
int z = 0;
for (int i =0;i<satirSayisi;i++){
for(int j=0;j<sutunSayisi&&z<satirSayisi*sutunSayisi;j++,z++){
[self textViewBicimle:[hucreler objectAtIndex:z]];
[[hucreler objectAtIndex:z] setFrame:CGRectMake((20+j*100), (20+i*40), 100, 40)];
}
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
}
-(void)hucreArrayOlustur: (int)sutunSayisi suankiView:(UIView *)soruViewIki satir:(int)satirSayisi{
for (int i = 0; i <sutunSayisi*satirSayisi; i++){
textView = [[UITextField alloc]init];
textView.text = #"dsgfdh";
[hucreArray addObject:textView];
[soruViewIki addSubview:textView];
}
}
-(void)viewOlustur{
for (int i = 0; i <soruAdedi; i++){
hucreArray = [[NSMutableArray alloc]init];
if (i == 0){
soruView = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 728, 0)];
soruView.clipsToBounds = YES;
soruView.backgroundColor = [UIColor lightGrayColor];
[self hucreArrayOlustur:5 suankiView:soruView satir:10];
[self tableCiz:hucreArray sutun:5 satir:10];
CGRect frame = soruView.frame;
CGRect frame2 = [[hucreArray objectAtIndex:49] frame];
frame.size.height = frame2.origin.y+frame2.size.height+34;
soruView.frame = frame;
[viewArray addObject:soruView];
[scrollView2 addSubview:soruView];
}
else{
CGRect frameGecici = [[viewArray objectAtIndex:i-1] frame];
float geciciY = frameGecici.origin.y+frameGecici.size.height+1;
soruView = [[UIView alloc]initWithFrame:CGRectMake(20, geciciY, 728, 0)];
soruView.clipsToBounds = YES;
soruView.backgroundColor = [UIColor lightGrayColor];
[self hucreArrayOlustur:5 suankiView:soruView satir:10];
[self tableCiz:hucreArray sutun:5 satir:10];
CGRect frame = soruView.frame;
CGRect frame2 = [[hucreArray objectAtIndex:49] frame];
frame.size.height = frame2.origin.y+frame2.size.height+34;
soruView.frame = frame;
[viewArray addObject:soruView];
[scrollView2 addSubview:soruView];
}
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView2.delegate = self;
scrollView2.contentSize = CGSizeMake(0, 8000);
viewArray = [[NSMutableArray alloc]init];
[self viewOlustur];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
[self setScrollView2:nil];
[super viewDidUnload];
}
#end
I'm kinda stuck trying to see why, when I use this custom UIView to show a row of 3 x UILabels, that whilst I see the outline in RED for the frame, I don't see anything inside the frame. That is the labels don't appear, nor does the black border for the UILabels which I've set.
Can anyone spot an issue here?
#import <UIKit/UIKit.h>
#interface LabelRowView : UIView {
UILabel *_dateLabel;
UILabel *_titleLabel;
UILabel *_locationLabel;
}
#property (nonatomic, retain) UILabel *dateLabel;
#property (nonatomic, retain) UILabel *titleLabel;
#property (nonatomic, retain) UILabel *locationLabel;
- (void)setFont:(UIFont *)newFont;
- (void)setDataWithDate:(NSString*)dateStr AndTitle:(NSString*)titleStr AndLocation:(NSString*)locationStr;
#end
// ==============================================================
#implementation LabelRowView
#synthesize dateLabel = _dateLabel;
#synthesize titleLabel = _titleLabel;
#synthesize locationLabel = _locationLabel;
- (void)setDefaultsForLabel:(UILabel*)label {
label.layer.borderColor = [UIColor blackColor].CGColor;
label.layer.borderWidth = 1.0;
label.textAlignment = UITextAlignmentLeft;
label.lineBreakMode = UILineBreakModeTailTruncation;
label.opaque = YES; // TODO: put back in for performance reasons if possible
label.backgroundColor = [UIColor clearColor];
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.dateLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease]; // Position will be updated later
self.titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease]; // Position will be updated later
self.locationLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease]; // Position will be updated later
// Set Defaults
[self setDefaultsForLabel:self.dateLabel];
[self setDefaultsForLabel:self.titleLabel];
[self setDefaultsForLabel:self.locationLabel];
self.layer.borderColor = [UIColor redColor].CGColor;
self.layer.borderWidth = 1.0;
}
return self;
}
- (void)setDataWithDate:(NSString*)dateStr AndTitle:(NSString*)titleStr AndLocation:(NSString*)locationStr {
self.dateLabel.text = dateStr;
self.titleLabel.text = titleStr;
self.locationLabel.text = locationStr;
}
- (void)setFont:(UIFont *)newFont {
self.dateLabel.font = newFont;
self.titleLabel.font = newFont;
self.locationLabel.font = newFont;
}
- (void)layoutSubviews {
[super layoutSubviews];
// Get current width for this subview
CGFloat contentViewWidth = self.frame.size.width;
CGSize maximumLabelSize = CGSizeMake(contentViewWidth, 9999);
CGFloat oneLineHeight = [#"A" sizeWithFont:self.dateLabel.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation].height;
// Calculate Widths
CGFloat dateColumnWidth = [#"Ddd 12:22 " sizeWithFont:self.dateLabel.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation].width;
CGFloat titleColumnWidth = (contentViewWidth - dateColumnWidth) * 2.0/3.0;
CGFloat locationColumnWidth = contentViewWidth - dateColumnWidth - titleColumnWidth;
// Date - Layout
CGRect newRect = CGRectMake(0.0, 0.0, dateColumnWidth, oneLineHeight);
self.dateLabel.frame = newRect;
// Date - Layout
newRect = CGRectMake(dateColumnWidth, 0.0, titleColumnWidth, oneLineHeight);
self.titleLabel.frame = newRect;
// Title - Layout
newRect = CGRectMake(dateColumnWidth + titleColumnWidth, 0.0, locationColumnWidth, oneLineHeight);
self.locationLabel.frame = newRect;
}
- (void)dealloc
{
[super dealloc];
}
#end
You don't seem to be adding the UILabels as subviews.
Here is the idea:
The scroll view has several sub views, in this example, A, B, C:
[A][B][C]
Once the user scrolls to C, the view can't scroll anymore, but I want to loop back to A, like this:
[C][A][B]
When the user keeps scrolling to the right, the view keeps filling previous views in at the end. When the user
scrolls to left the view should also have similar behavior,
in order to make the user think that this view is infinitely long:
[A][B][C][A][B][C][A][B][C][A][B][C][A][B][C][A][B][C].....
How can I implement this in actual code? Thank you.
you need to set a delegate method that gets called when the view is scrolled, and then check any view that is more than one screens worth of pixels away from being visible, if it is then reposition the view 3*screen width to the other side.
This is what you want( on iphone 7 plus). This is pure code UI, no storyboard.First you need add sub code in your AppDelegate.m.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
ViewController *vc = [ViewController alloc];
self.window.rootViewController = vc;
self.window.backgroundColor = [UIColor redColor];
return YES;
}
Second add sub code in ViewController.m
#interface ViewController ()<UIScrollViewDelegate>
#property (nonatomic, strong) UIScrollView *readCannelScrollView;
#property (nonatomic, strong) UIImageView *pageOneView;
#property (nonatomic, strong) UIImageView *pageTwoView;
#property (nonatomic, strong) UIImageView *pageThreeView;
#end
#implementation ViewController
- (void)dealloc
{
_readCannelScrollView = nil;
_pageOneView = nil;
_pageTwoView = nil;
_pageThreeView = nil;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//容器的属性设置
self.readCannelScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.readCannelScrollView];
CGSize size = self.readCannelScrollView.contentSize;
size.width = 414*3;
self.readCannelScrollView.contentSize = size;
self.readCannelScrollView.pagingEnabled = YES;
self.readCannelScrollView.showsHorizontalScrollIndicator = NO;
self.readCannelScrollView.delegate = self;
self.readCannelScrollView.contentOffset = CGPointMake(0, 0);
//end
//添加页面1
self.pageOneView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 414, self.view.bounds.size.height)];
self.pageOneView.backgroundColor = [UIColor lightGrayColor];
self.pageOneView.image = [UIImage imageNamed:#"1"];
// self.pageOneView.font = [UIFont systemFontOfSize:80];
// self.pageOneView.textAlignment = NSTextAlignmentCenter;
[self.readCannelScrollView addSubview:self.pageOneView];
//添加页面2
self.pageTwoView = [[UIImageView alloc] initWithFrame:CGRectMake(828, 0, 414, self.view.bounds.size.height)];
self.pageTwoView.backgroundColor = [UIColor greenColor];
self.pageTwoView.image = [UIImage imageNamed:#"2"];
// self.pageTwoView.font = [UIFont systemFontOfSize:80];
// self.pageTwoView.textAlignment = NSTextAlignmentCenter;
[self.readCannelScrollView addSubview:self.pageTwoView];
//添加页面3
self.pageThreeView = [[UIImageView alloc] initWithFrame:CGRectMake(828, 0, 414, self.view.bounds.size.height)];
self.pageThreeView.backgroundColor = [UIColor grayColor];
self.pageThreeView.image = [UIImage imageNamed:#"3"];
// self.pageThreeView.font = [UIFont systemFontOfSize:80];
// self.pageThreeView.textAlignment = NSTextAlignmentCenter;
[self.readCannelScrollView addSubview:self.pageThreeView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark -
#pragma mark - scroll delegate
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(#"scrollView.contentOffset.x=%f",scrollView.contentOffset.x);
CGFloat pageWidth = scrollView.frame.size.width;
int currentPage = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
if (currentPage == 0)
{
UIImageView *tmpTxtView = self.pageThreeView;
self.pageThreeView = self.pageTwoView;
self.pageTwoView = self.pageOneView;
self.pageOneView = tmpTxtView;
}
if (currentPage == 2)
{
//换指针
UIImageView *tmpTxtView = self.pageOneView;
self.pageOneView = self.pageTwoView;
self.pageTwoView = self.pageThreeView;
self.pageThreeView = tmpTxtView;
}
//恢复原位
self.pageOneView.frame = (CGRect){0,0,self.pageOneView.frame.size};
self.pageTwoView.frame = (CGRect){414,0,self.pageTwoView.frame.size};
self.pageThreeView.frame = (CGRect){828,0,self.pageThreeView.frame.size};
self.readCannelScrollView.contentOffset = CGPointMake(414, 0);
}
#end
If you are satisfied with my answer give me a star.I need reputation.