How to bring an image to front in ScrollView - ios

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

Related

UIButton not displayed

I use the following code to add a UIButton and a UITableView to a UIViewController:
self.tableView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
tableView.scrollEnabled = NO;
tableView.allowsSelection = NO;
tableView.dataSource = self;
tableView.delegate = self;
tableView;
});
self.refreshButton = ({
UIButton *button = [UIButton new];
button.translatesAutoresizingMaskIntoConstraints = NO;
[button setTitle:#"testtest" forState:UIControlStateNormal];
[button addTarget:self
action:#selector(clickRefreshButton)
forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.numberOfLines = 1;
button.titleLabel.textAlignment = NSTextAlignmentRight;
button.titleLabel.adjustsFontSizeToFitWidth = NO;
button.alpha = 1.0;
button;
});
[self.view addSubview:self.tableView];
[self.view addSubview:self.refreshButton];
CGFloat tableViewUpPadding = 20.0f;
CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
CGFloat buttonOffset = 10.0f;
CGFloat buttonSideLength = 50.0f;
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(self.view.mas_top).offset(tableViewUpPadding);
make.bottom.equalTo(self.view.mas_bottom).offset(-tabBarHeight);
make.width.equalTo(self.view.mas_width);
make.left.equalTo(self.view.mas_left);
}];
[self.refreshButton mas_makeConstraints:^(MASConstraintMaker *make){
make.right.equalTo(self.view.mas_right).offset(-buttonOffset);
make.bottom.equalTo(self.view.mas_bottom).offset(-buttonOffset);
make.width.equalTo(#(buttonSideLength));
make.height.equalTo(#(buttonSideLength));
}];
Among which I use masonry to do auto-layout. However when I run the app the button doesn't appear, and not even in view debugging area.. Can somebody tell me why?
Or if I use strings and struts to create button, it still won't show :(
CGRect applicationFrame = [UIScreen mainScreen].bounds;
CGFloat screenWidth = CGRectGetWidth(applicationFrame);
CGFloat screenHeight = CGRectGetHeight(applicationFrame);
CGFloat buttonWidth = 40.0f;
CGFloat buttonHeight = 40.0f;
CGFloat spacing = 10.0f;
CGFloat buttonX = screenWidth - buttonWidth - spacing;
CGFloat buttonY = screenHeight - buttonHeight - spacing;
CGRect buttonFrame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
[self.refreshButton setFrame:buttonFrame];
By the way, I found that if I want to change self.tableView's width using auto-layout, it's also impossible:
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(self.view.mas_top).offset(tableViewUpPadding);
make.bottom.equalTo(self.view.mas_bottom).offset(-tabBarHeight);
make.width.equalTo(self.view.as_width).offset(-offset);
make.left.equalTo(self.view.mas_left);
}];
It turns out that the tableview is still as wide as self.view

scrollView with paging enabled not working properly ? (Objective-C)

i want to create paging scrollView with three UIViews. And than wants to add imageView as a subView to these UIViews. i cant figure it out how to calculate the frame of imageViews.
When i run the code without imageViews it works perfectly fine.
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
self.scrollView.frame = self.view.frame;
NSArray *colorsArray = [NSArray arrayWithObjects:[UIColor blueColor], [UIColor yellowColor],[UIColor greenColor], nil];
NSArray *imagesArray = [NSArray arrayWithObjects:[UIImage imageNamed:#"123.png"],[UIImage imageNamed:#"13.png"],[UIImage imageNamed:#"12.png"],nil];
for (int i = 0; i < colorsArray.count; i++) {
CGRect frame;
frame.origin.x = self.view.frame.size.width * i;
frame.size = self.view.frame.size;
self.scrollView.pagingEnabled = YES;
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [colorsArray objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [imagesArray objectAtIndex:i];
[view addSubview:imageView];
[self.scrollView addSubview:view];
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * colorsArray.count, self.scrollView.frame.size.height);
self.pageControl.numberOfPages = 3;
self.pageControl.currentPage = 0;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = CGRectGetWidth(self.scrollView.bounds);
CGFloat pageFraction = self.scrollView.contentOffset.x / pageWidth;
self.pageControl.currentPage = roundf(pageFraction);
}
The frame for the image view needs to be relative to its superview (i.e. view) not the scroll view. To achieve the paging you desire change:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
to:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height)];

ScrollView for images Objective C (iOS)

I am trying to create a welcome page for my app (with screenshots).
I have taken some samples and made it into a text scroll. I have tried for weeks without any success. Thanks
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 0; i < numberOfPages; i++) {
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
20,
someScrollView.frame.size.width - 40,
20)];
tmpLabel.textAlignment = UITextAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i];
[someScrollView addSubview:tmpLabel];
}
Here is my failed attempt to do with images:
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 0; i < numberOfPages; i++) {
UIImageView *tmpLabel = [[UIImageView alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
20,
someScrollView.frame.size.width - 40,
20)];
tmpLabel.imageAlignment = UIImageAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i];
[someScrollView addSubview:tmpLabel];
}
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:subview1.frame];
if (i == 0) {
imageView1.image = [UIImage imageNamed:#"openingScreen1.png"];
}else if (i == 1){
imageView1.image = [UIImage imageNamed:#"openingScreen2.png"];
}else if (i == 2){
imageView1.image = [UIImage imageNamed:#"openingScreen3.png"];
}else {
imageView1.image = [UIImage imageNamed:#"openingScreen4.png"];
}
I also might need to remove the word "Parse" unless the images above sit on top of it? I cannot locate the word.
try this
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
someScrollView.pagingEnabled = YES;
[someScrollView setAlwaysBounceVertical:NO];
//setup internal views
NSInteger numberOfPages = 5;
for (int i = 0; i < numberOfPages; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width+10;
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOrigin,70,self.view.frame.size.width,25)];
tmpLabel.textAlignment = NSTextAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i+1];
[someScrollView addSubview:tmpLabel];
// calculate the width
UIImageView *image = [[UIImageView alloc] initWithFrame:
CGRectMake(xOrigin, tmpLabel.frame.size.height + tmpLabel.frame.origin.y + 10,
self.view.frame.size.width,
self.view.frame.size.height)]; // customize your width, height and y co ordinates
image.image = [UIImage imageNamed:[NSString stringWithFormat:
#"openingScreen%d.jpg", i+1]];
image.contentMode = UIViewContentModeScaleAspectFit;
[someScrollView addSubview:image];
}
//set the scroll view content size
someScrollView.backgroundColor = [UIColor blueColor];
someScrollView.contentSize = CGSizeMake(self.view.frame.size.width *
numberOfPages,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:someScrollView];
the sample OutPut is
here I attached the sample Project also, please use this
Please change your code to something like
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
someScrollView.pagingEnabled = YES;
//set the scroll view delegate to self so that we can listen for changes
someScrollView.delegate = self;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 1; i <= numberOfPages; i++) {
//set the origin of the sub view
CGFloat myOrigin = i * self.view.frame.size.width;
//get the sub view and set the frame
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, someScrollView.frame.size.height)];
imageView1.image = [UIImage imageNamed: [NSString stringWithFormat:#"openingScreen%d.png",i ]];
//add the subview to the scroll view
[someScrollView addSubview:imageView1];
}
Add scroll view Delegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
}
//dragging ends, please switch off paging to listen for this event
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *) targetContentOffset NS_AVAILABLE_IOS(5_0)
{
//find the page number you are on
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSLog(#"Dragging - You are now on page %i",page);
}

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)
}

How to add images in nested UIScrollview

I need to add images in scroll view multiple times. Am created a scrollview but images not append correctly in that.
My code is here :
-(void)sampleScroll
{
int x = 10;
int y = 20;
mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
mainScrollView.contentSize = CGSizeMake(50, (y + 95) * 5);
// further configure
[self.view addSubview: mainScrollView];
images = [[NSMutableArray alloc] initWithObjects:#"image0.jpg",#"image1.jpg",#"image2.jpg",#"image3.jpg", nil];
for(int i=0; i<[images count]; i++)
{
NSLog(#"%#",images);
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 250, 150)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
scrollview.backgroundColor = [UIColor whiteColor];
NSString *img = [images objectAtIndex:i];
NSLog(#"%#",img);
imageView.image = [UIImage imageNamed:#"image3.jpg"];
NSLog(#"%#",imageView.image);
scrollview.contentSize = CGSizeMake(1250,250);
[scrollview addSubview:imageView];
[mainScrollView addSubview:scrollview];
y=y+155;
//[self myscrollView];
}
}
Please give me a solution. Thanks in advance..
Frame adjustment needed,
int x = 10;
int y = 10;
UIScrollView * mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
mainScrollView.contentSize = CGSizeMake(mainScrollView.frame.size.width, mainScrollView.frame.size.height * COUNTS);
mainScrollView.pagingEnabled = YES;
for(int i = 0; i < COUNTS; i++)
{
UIScrollView * scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(x, y, mainScrollView.frame.size.width - 20.0, mainScrollView.frame.size.height - 20.0)];
scrollview.showsVerticalScrollIndicator = YES;
scrollview.scrollEnabled = YES;
scrollview.userInteractionEnabled = YES;
scrollview.backgroundColor = [UIColor whiteColor];
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(x,10.0, mainScrollView.frame.size.width - 40.0, mainScrollView.frame.size.height - 40.0)];
imageView.image = [UIImage imageNamed:#"owl.jpg"];
[scrollview addSubview:imageView];
[mainScrollView addSubview:scrollview];
y += mainScrollView.frame.size.height;
}
[self.view addSubview:mainScrollView];

Resources