Fix next image in scroll view like in page view - ios

I created a scroll view , but i want to made it to act like a page view .
- (void)setupScrollView:(UIScrollView*)scrMain {
for (int i=1; i<=11; i++) {
image = [UIImage imageNamed:[NSString stringWithFormat:#"%d.png",i]];
if (i==1) {
imgV = [[UIImageView alloc] initWithFrame:CGRectMake(20, 2, 289, 470)];
}
else
imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*350, 2, 289, 470)];
imgV.contentMode=UIViewContentModeScaleToFill;
[imgV setImage:image];
imgV.tag=i+1;
[imgV bringSubviewToFront:self.aleatoire];
[scrMain addSubview:imgV];
UIImageView *aleatoire=[[UIImageView alloc]initWithFrame:CGRectMake (self.aleatoire.frame.origin.x-19,self.aleatoire.frame.origin.y,self.aleatoire.frame.size.width,self.aleatoire.frame.size.height)];
aleatoire.image=self.aleatoire.image;
[imgV addSubview:aleatoire];
[imgV bringSubviewToFront:aleatoire];
UIButton *rand = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[rand addTarget:self
action:#selector(randomBtn:)
forControlEvents:UIControlEventTouchUpInside];
rand.frame=aleatoire.frame;
rand.backgroundColor=[UIColor clearColor];
rand.userInteractionEnabled = YES;
imgV.userInteractionEnabled=YES;
[imgV addSubview:rand];
[imgV bringSubviewToFront:rand];
}
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width*11, scrMain.frame.size.height)];
}
but i have this issue:
the next image is not in the center . Any ideea where i'am wrong ?

You set UIScrollView paging pagingEnabled property enable to create scroll behavior like pageView.
try this code :
scrMain.pagingEnabled=YES;
scrMain.contentOffset = CGPointMake([[UIScreen mainScreen] bounds].size.width, 0); // set your size to scroll content
Simple way to create pagerView in UIScrollView
int x = 0;
for (int i=1; i<=11; i++) {
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height)];
img.image = [UIImage imageNamed:[NSString stringWithFormat:#"%d.png",i]];
[self.scrollView addSubview:img];
x = x + [[UIScreen mainScreen] bounds].size.width;
}
self.scrollView.contentSize = CGSizeMake(x, [[UIScreen mainScreen] bounds].size.height-64);
self.scrollView.pagingEnabled = YES;
self.scrollView.contentOffset = CGPointMake([[UIScreen mainScreen] bounds].size.width, 0);

Related

How to bring an image to front in ScrollView

I'm showing images in the ScrollView but I want to know please how can I bring each image to the front in the middle while I'm scrolling. Also it is on the back on each other as in the picture below.
My code:
-(void)scrollView2{
_scrlView.backgroundColor = [UIColor blackColor];
_scrlView.pagingEnabled = NO;
_scrlView.bounces = false;
_scrlView.delegate = self;
_scrlView.showsHorizontalScrollIndicator = YES;
_scrlView.layer.cornerRadius = 2;
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width, _scrlView.frame.size.height);
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 2, _scrlView.frame.size.height);
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
int segment = screenWidth / 5;
int screenX2 = screenHeight/2 - segment/2;
__block float x = (screenWidth-segment)/2;
NSArray *arrImages=[NSArray arrayWithObjects:#"Smiley-1.png",#"Smiley-2.png",#"Smiley-3.png",#"Smiley-4.png",#"Smiley-5.png", nil];
for(int i=0;i<arrImages.count;i++)
{
UIImageView *imgVw=[[UIImageView alloc]init];
[imgVw setFrame:CGRectMake(x, screenX2, segment, segment)];
[imgVw setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",[arrImages objectAtIndex:i]]]];
[imgVw setBackgroundColor:[UIColor redColor]];
[imgVw setContentMode:UIViewContentModeScaleToFill];
[_scrlView addSubview:imgVw];
x=x+segment;
}
[self.view addSubview:_scrlView];
}
float oldY;
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int scrollX = scrollView.contentOffset.x;
int position = scrollX/segment;
NSLog(#"X: %f , position: %d", scrollView.contentOffset.x , position);
[_scrlView setContentOffset: CGPointMake(_scrlView.contentOffset.x, oldY)];
}
You can use bring sub view to front. For instance
[scrollView bringSubviewToFront:_img1];
I understood what you meant, there is an interesting project. It should help you.
You can also try yourself using a UICollectionView.
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *arrImages=[NSArray arrayWithObjects:#"smiley1",#"smiley2",#"smiley3",#"smiley4",#"smiley5",#"smiley6", #"smiley7",#"smiley8", nil];
screenRect = [[UIScreen mainScreen] bounds];
screenWidth = screenRect.size.width;
screenHeight = screenRect.size.height;
UIScrollView *scrlView=[[UIScrollView alloc]init];
[scrlView setFrame:CGRectMake(0, 0, 320, 568)];
scrlView.backgroundColor = [UIColor blackColor];
scrlView.pagingEnabled = YES;
scrlView.bounces = false;
scrlView.delegate = self;
scrlView.showsHorizontalScrollIndicator = YES;
scrlView.layer.cornerRadius = 2;
segmentWidth = screenWidth / 5;
long ScrollContentSize = ((arrImages.count -1)*segmentWidth)+screenWidth;
scrlView.contentSize = CGSizeMake(ScrollContentSize, scrlView.frame.size.height);
float screenX2 = screenHeight/2 - segmentWidth/2;
__block float screenX = (screenWidth-segmentWidth)/2;
for(int i=0;i<arrImages.count;i++)
{
UIImageView *imgVw=[[UIImageView alloc]init];
[imgVw setFrame:CGRectMake(screenX, screenX2, segmentWidth+30, segmentWidth+30)];
[imgVw setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",[arrImages objectAtIndex:i]]]];
[imgVw setTag:5000+i];
[imgVw setContentMode:UIViewContentModeScaleAspectFit];
screenX = screenX+segmentWidth;
[scrlView addSubview:imgVw];
if (i%2==0)
{
[imgVw setBackgroundColor:[UIColor clearColor]];
[scrlView sendSubviewToBack:imgVw ];
}
else
{
[imgVw setBackgroundColor:[UIColor clearColor]];
[scrlView bringSubviewToFront:imgVw];
}
}
[self.view addSubview:scrlView];
}
Try this

UIScrollView Not scrolling to the bottom

I have a scrollview set up that holds 20 sunglasses, when calling the function, it brings up a new view with the sunglasses 2 by 2 with 20 in total (with 19 and 20 being at the bottom of the scrollview).
It works perfectly on iPhone & iPhone5 but on the iPad, it only goes to 16, if I try and drag further, I can see the remaining 4 sunglasses, but I cannot select them, as the scrollview just bounces back to the 'bottom' which it thinks is 16.
This is on an iPad Air, so I am not sure if it an iPad 2 would have this issue with the different resolution.
This is how I am calling the view;
- (IBAction)glassesClick:(id)sender
{
[(AppDelegate*)[[UIApplication sharedApplication] delegate] playSoundEffect:1];
[scroll scrollRectToVisible:CGRectMake(0, 0, 5, 5) animated:NO];
glassesView.frame = CGRectMake(0, -[[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
[self.view addSubview:glassesView];
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^
{
glassesView.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
} completion:nil];
}
Scroll container view setting;
{
for(UIView *view in [scroll subviews])
[view removeFromSuperview];
scroll.contentSize = CGSizeMake(scroll.frame.size.width, 1760);
for(int i = 0; i < 20; i++)
{
int xCoord = 0;
if(i % 2 == 0)
xCoord = 57;
else
xCoord = 188;
UIButton *glasss = [[UIButton alloc] initWithFrame:CGRectMake(xCoord, 20 + i/2 * 150 + i/2 * 20, 75, 150)];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
int xCoord = 0;
if(i % 2 == 0)
xCoord = 156;
else
xCoord = 462;
glasss.frame = CGRectMake(xCoord, 20 + i/2 * 300 + i/2 * 20, 150, 300);
scroll.contentSize = CGSizeMake(scroll.frame.size.width, 2580);
}
glasss.tag = i + 1;
[glasss addTarget:self action:#selector(glassChosen:) forControlEvents:UIControlEventTouchUpInside];
[glasss setImage:[UIImage imageNamed:[NSString stringWithFormat:#"glass%d.png", i + 1]] forState:UIControlStateNormal];
[[glasss imageView] setContentMode:UIViewContentModeScaleAspectFit];
if(i >= 10)
{
if([[[NSUserDefaults standardUserDefaults] objectForKey:#"purchased_glasses"] isEqualToString:#"NO"])
{
UIImageView *locked = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 25)];
locked.image = [UIImage imageNamed:#"lock_button.png"];
[glasss addSubview:locked];
[locked release];
[glasss removeTarget:self action:#selector(glassChosen:) forControlEvents:UIControlEventTouchUpInside];
[glasss addTarget:self action:#selector(chooseLockedFlavor) forControlEvents:UIControlEventTouchUpInside];
}
}
UIImageView *backGlass = [[UIImageView alloc] initWithFrame:glasss.frame];
backGlass.image = [UIImage imageNamed:#"back_glass.png"];
[backGlass setContentMode:UIViewContentModeScaleAspectFit];
[scroll addSubview:backGlass];
[backGlass release];
[scroll addSubview:glasss];
[glasss release];
}
}
In this segment:
scroll.contentSize = CGSizeMake(scroll.frame.size.width, 1760);
We discovered you're explicitly setting the height of the contentSize. As such, you're probably blocking your iPad view. Try setting it dynamically (or not setting it at all!).
Set scrollView contentSize in viewDidLayoutSubviews instead of viewDidLoad.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scroll.contentSize = CGSize(scroll.frame.size.width, 1760)
}

UIView subView button not active

I'm want to add button into UIImageView. Added button not active.
UIImageView *iview = [[UIImageView alloc] initWithImage:image];
iview.frame = [[UIScreen mainScreen] applicationFrame];
iview.contentMode = UIViewContentModeTopLeft;
UIButton *bview1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
bview1.frame = CGRectMake(0, 0, 200, 60);
bview1.center = CGPointMake(100, 100);
[bview1 addTarget:self action:#selector(buttonPress1:) forControlEvents:UIControlEventTouchUpInside];
[bview1 setTitle:#"Image Picker1" forState:UIControlStateNormal];
[iview addSubview:bview1];
[self.view addSubview:iview];
Handler:
- (void)buttonPress1:(id)sender
{
NSLog(#"1");
}
What's wrong?
Did you try:
iview.userInteractionEnabled = YES;

UIScrollView With Pictures Generated

I have set up a UIScrollView and loaded in pictures and set it equal to the view with some offset between the pictures. Can someone possibly explain what I might have done wrong? It shows the first picture just fine but wont let me scroll left and right to see the next ones. Do I need a gesture recognizer with the new XCode to make this work?
- (void)viewDidLoad
{
[super viewDidLoad];
int PageCount = 3;
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
Scroller.contentSize = CGSizeMake(PageCount = Scroller.bounds.size.width, Scroller.bounds.size.height);
CGRect ViewSize = Scroller.bounds;
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView setImage:[UIImage imageNamed:#"1.png"]];
[Scroller addSubview:ImgView];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView2 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView2 setImage:[UIImage imageNamed:#"2.png"]];
[Scroller addSubview:ImgView2];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView3 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView3 setImage:[UIImage imageNamed:#"3.png"]];
[Scroller addSubview:ImgView3];
[self.view addSubview:Scroller];
}
#end
You need to manually set the contentSize of your ScrollView to the maximum rectangle which fits all the ImageViews you added. For example : Lets say you want to scroll right and left and you have 4 views with each view having width 100 and you have offset of 10 in x direction. Then after adding all the 4 views to your scrollView, you will have to make the contentSize as below :
scrollView.contentSize = CGSizeMake( 10 + (100 + 10)*4 , scrollView.contentSize.y );
This will make the scrollView scrollable and you will see all the views.
Try this
int PageCount = 3;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png", nil];
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.scrollEnabled=YES;
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
[self.view addSubview:Scroller];
int width=Scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, Scroller.frame.size.width, Scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[Scroller addSubview:ImgView];
Scroller.contentSize = CGSizeMake(width, 0);
width +=Scroller.frame.size.width;
xPos +=Scroller.frame.size.width;
}

Image in full screen from scroll view

I have a UIScrollView and have images in that ScrollView and they are added programmatically. When I click on image, I want that image to be displayed on full screen.
What should I add to my code?
The number of images depend upon the [images count];
self.scrollView.contentSize = CGSizeMake(320, 134);
self.scrollView.delegate = self;
int X=0;
for (int i = 0; i < [images count]; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, 140, 136)] ;
imageView.backgroundColor = [UIColor redColor];
[imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
[imageView setImage: [images objectAtIndex:i]];
[self.scrollView addSubview: imageView];
X = X + imageView.frame.size.height+5;
if(X > 320)
self.scrollView.contentSize = CGSizeMake(X, 134);
}
You should not add UIImageViews then.
Instead add UIButton of type Custom and add the Image to the Button and add an IBAction to the UIButtons which adds a FullScreenView with your image.
In your for() for each button add a tag with i and in your IBAction function get the tag with [sender tag] and get your image with [images objectAtIndex:[sender tag]]
Example:
-(void)test{
for (int i = 0; i < [images count]; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(X, 0, 140, 136);
[button setImage:[images objectAtIndex:i] forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:#selector(showFullscreen:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
X = X + button.frame.size.height+5;
if(X > 320)
self.scrollView.contentSize = CGSizeMake(X, 134);
}
}
-(IBAction)showFullscreen:(id)sender{
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)] ;
imageView.backgroundColor = [UIColor redColor];
[imageView setImage: [images objectAtIndex:[sender tag]]];
[self.view addSubview: imageView];
}

Resources