Z position of UIImageView to that of UIScrollView - ios

I have both UIScrollView and another UIImageView. I want the UIImageView to come over the UIScrollView. I've tried all the bringSubviewToFront and the insertAtIndex stuff but its not working. Please help me out!
Here is the code -
For UIScrollView:
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 300,
SCREEN_WIDTH, SCREEN_HEIGHT)];
scrollView.showsHorizontalScrollIndicator = FALSE;
[self.view addSubview:scrollView];
__block int tagValue = 1;
__block NSInteger tag = 1;
for (int i=0; i<[listOfImages count]; i++) {
NSDictionary *myDic = [listOfImages objectAtIndex:i];
NSString *urlImage = [myDic objectForKey:#"product_image"];
//NSLog(#"%lu",(unsigned long)[listOfImages count]);
image = [[UIImageView alloc] initWithFrame:CGRectMake(leftMargin, 0, 200, 140)];
// [image setImage:[UIImage imageNamed:#"img_def.png"]];
NSURL *imageURL = [NSURL URLWithString:urlImage];
[image setImageWithURL:imageURL
placeholderImage:[UIImage imageNamed:#"img_def.png"]];
image.tag = tag;
image.contentMode = UIViewContentModeScaleAspectFit;
[scrollView insertSubview:image atIndex:1];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleTap:)];
recognizer.numberOfTapsRequired = 1;
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[image addGestureRecognizer:recognizer];
[image setUserInteractionEnabled:YES];
leftMargin += SCREEN_WIDTH;
tagValue += 1;
tag += 1;
}
[scrollView setContentSize:CGSizeMake(leftMargin, 0)];
[scrollView setPagingEnabled:YES];
And the image code that I want to come on top of the scroll view -
UIImage *originalPromo = [UIImage imageNamed:#"promo"];
UIImage *scaledPromo = [UIImage imageWithCGImage:[originalPromo CGImage]
scale:(originalPromo.scale *2.0) orientation:(originalPromo.imageOrientation)];
UIImageView *promo = [[UIImageView alloc] init];
[promo setFrame:CGRectMake(45.5, 300.0,
scaledPromo.size.width, scaledPromo.size.height)];
[promo setImage:scaledPromo];
[self.view insertSubview:promo atIndex:100];

Instead of using insertSubview:atIndex use addSubview:. When a subview is added, it's always added on top of all other subviews already in the parent view.

Related

how to get current image tag in scroll view?

//add the scrollview to the view
Friend_Request_Scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-60,70,120,140)];
[Friend_Request_Scroll setAlwaysBounceVertical:NO];
Friend_Request_Scroll.delegate=self;
Friend_Request_Scroll.pagingEnabled=YES;
[Friend_Request_Scroll setShowsHorizontalScrollIndicator:NO];
[Friend_Request_Scroll setShowsVerticalScrollIndicator:NO];
CGFloat xOrigin = 0;
for (int i = 0; i <[imageCollection count]; i++) {
Back_Request_Scroll=[[UIView alloc]init];
Back_Request_Scroll.frame=CGRectMake(xOrigin,Friend_Request_Scroll.frame.origin.y-135,120,140);
Back_Request_Scroll.backgroundColor=[UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:0.5];
Back_Request_Scroll.clipsToBounds=YES;
[Friend_Request_Scroll addSubview:Back_Request_Scroll];
image = [[UIImageView alloc] initWithFrame:
CGRectMake(0, 0,
Back_Request_Scroll.frame.size.width,
Back_Request_Scroll.frame.size.height)];
[image sd_setImageWithURL:[imageCollection objectAtIndex:i] placeholderImage:[UIImage imageNamed:#""]];
image.tag=i;
[Back_Request_Scroll addSubview:image];
xOrigin= xOrigin+120;
image.userInteractionEnabled=YES;
}
Friend_Request_Scroll.contentSize = CGSizeMake(xOrigin,
0);
[self.view addSubview:Friend_Request_Scroll];
swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[image addGestureRecognizer:swipeleft];
swiperight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swiperight:)];
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[image addGestureRecognizer:swiperight];
pageControl = [[UIPageControl alloc] init];
pageControl.frame = CGRectMake(self.view.frame.size.width/2-60,Friend_Request_Scroll.frame.size.height+Friend_Request_Scroll.frame.origin.y,image.frame.size.width,20);
pageControl.numberOfPages = [imageCollection count];
pageControl.currentPage = currentValue;
[pageControl addTarget:self action:#selector(changePage) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];
Try this Delegate Method.This delegate will tell you that scrolling has finished.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
Try this code, you use recongizer object view tag.
- (void)viewDidLoad
{
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:scroll];
NSInteger i;
for (i=0; i<20; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*90 + i*15, 300, 90)];
imageView.backgroundColor = [UIColor blackColor];
imageView.userInteractionEnabled = YES;
imageView.tag = i;
NSLog(#“image Tag = %d”, imageView.tag);
[scroll addSubview:imageView];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];
[imageView addGestureRecognizer: recognizer];
}
scroll.contentSize = CGSizeMake(320, 110*i);
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.");
NSLog(#“image Tag = %d”, recognizer.view.tag);
}
Update
No need swipe.
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (sender == scroll) {
int pageNum = (int)(scroll.contentOffset.x / scroll.frame.size.width);
NSLog(#"%d",pageNum);
//self.pagecontroller.currentPage =pageNum;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
scroll.delegate = self;
scroll.pagingEnabled = YES;
[self.view addSubview:scroll];
CGFloat currentXOffset = 0;
NSInteger i;
for (i=0; i<3; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(currentXOffset, 100, self.view.frame.size.width, 200)];
if (i == 0){
imageView.backgroundColor = [UIColor blackColor];
}else if (i == 1){
imageView.backgroundColor = [UIColor orangeColor];
}else if (i == 2){
imageView.backgroundColor = [UIColor grayColor];
}
imageView.userInteractionEnabled = YES;
imageView.tag = i;
currentXOffset = currentXOffset + imageView.frame.size.width;
[scroll addSubview:imageView];
// UISwipeGestureRecognizer *recognizer;
//
// recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
// [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];
// [imageView addGestureRecognizer: recognizer];
}
scroll.contentSize = CGSizeMake(currentXOffset, self.view.frame.size.height);
}
Add tap gesture action to the image and you can get the tag using [sender tag]
I have Few ImageView each of them have tag and I have an array of images. when user tap on image then I get image tag and take action on it.
- (void)viewDidLoad
{
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:scroll];
NSInteger i;
for (i=0; i<20; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*90 + i*15, 300, 90)];
imageView.backgroundColor = [UIColor blackColor];
imageView.userInteractionEnabled = YES;
imageView.tag = i;
NSLog(#“image Tag = %d”, imageView.tag);
[scroll addSubview:imageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(findTheTag:)];
[imageView addGestureRecognizer:tap];
}
scroll.contentSize = CGSizeMake(320, 110*i);
}
- (void)findTheTag:(id)sender
{
NSLog(#“image Tag = %d”, sender.tag);
}

IOS 8 ScrollView fixed content

Hi I am developing an IOS application. Also using scrollview. As I encountered a problem on the iOS version 8. I added in the following way scroll the content inside. Scroll back in the first picture looks steady. Other images normally scrolling.
NSArray *aImageUrls = (NSArray *)[_dataSource arrayWithImageUrlStrings];
if([aImageUrls count] > 0)
{
[_scrollView setContentSize:CGSizeMake(_scrollView.frame.size.width * [aImageUrls count],
_scrollView.frame.size.height)];
for (int i = 0; i < [aImageUrls count]; i++)
{
CGRect imageFrame = CGRectMake(_scrollView.frame.size.width * i, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
[imageView setBackgroundColor:[UIColor clearColor]];
[imageView setContentMode:[_dataSource contentModeForImage:i]];
[imageView setTag:i];
[imageView setImageWithURL:[NSURL URLWithString:(NSString *)[aImageUrls objectAtIndex:i]]];
// Add GestureRecognizer to ImageView
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(imageTapped:)];
[singleTapGestureRecognizer setNumberOfTapsRequired:1];
[imageView addGestureRecognizer:singleTapGestureRecognizer];
[imageView setUserInteractionEnabled:YES];
[_scrollView addSubview:imageView];
}
[_countLabel setText:[NSString stringWithFormat:#"%lu", (unsigned long)[[_dataSource arrayWithImageUrlStrings] count]]];
_pageControl.numberOfPages = [(NSArray *)[_dataSource arrayWithImageUrlStrings] count];
_pageControl.hidden = ([(NSArray *)[_dataSource arrayWithImageUrlStrings] count] > 0?NO: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;
}

IOS scroll gallery

i made this sliding gallery in my app:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(photoTap:)];
tap.numberOfTapsRequired = 1;
int pageCount = 3;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,
0,
360,
200)];
scrollView.backgroundColor = [UIColor clearColor];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(pageCount*scrollView.bounds.size.width ,
scrollView.bounds.size.height);
[scrollView addGestureRecognizer:tap];
CGRect viewsize = scrollView.bounds;
UIImageView *image = [[UIImageView alloc]initWithFrame:viewsize];
[image setImage: [UIImage imageNamed:#"01.png"]];
image.userInteractionEnabled = YES;
[scrollView addSubview:image];
viewsize = CGRectOffset(viewsize, scrollView.bounds.size.width, 0);
UIImageView *image2 = [[UIImageView alloc]initWithFrame:viewsize];
[image2 setImage: [UIImage imageNamed:#"02.png"]];
image2.userInteractionEnabled = YES;
[scrollView addSubview:image2];
- (void)photoTap:(UITapGestureRecognizer *) gestureRecognizer{
}
How can i detect which photo is tapped in my scrollview?
I have tried to add gesture recognizer to the image but only the last one works.
Any suggestion? Thank You
You can add gesture recognizer to the scrollview, after detecting the tap, detect its location on the UIScrollView's content by adding the coordinates of the tap to the content offset. When you have this coordinate i think that detecting the image is not a problem.

uibutton is not getting displayed

I am trying to add uibutton inside the imageview.
But it was not displayed the button.
Even i tried adding it to the uiscrollview and also for self.view.
But nothing were displayed the uibutton
Pls let me know what is the problem
const CGFloat HEIGHT = 1024.0;
const CGFloat WIDTH = 768.0;
#define myViewPortrait CGRectMake(0.0f, 0.0f, 768.0f,1024.0f)
#define myViewLandSacpe CGRectMake(0.0f, 0.0f, 1024.0f,768.0f)
#define kAnimationKey #"animationKey"
-(void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [myScrollView subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (self.view.frame.size.width);
}
}
[myScrollView setContentSize:CGSizeMake((myImagesCount * self.view.frame.size.width), [myScrollView bounds].size.height)];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
myScrollView = [[UIScrollView alloc] initWithFrame:
CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:myScrollView];
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightRecognizer.numberOfTouchesRequired = 1;
[rightRecognizer setDelegate:self];
[myScrollView addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
leftRecognizer.numberOfTouchesRequired = 1;
[leftRecognizer setDelegate:self];
[myScrollView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
[myScrollView setBackgroundColor:[UIColor blackColor]];
[myScrollView setCanCancelContentTouches:NO];
myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
myScrollView.clipsToBounds = YES;
myScrollView.scrollEnabled = YES;
myScrollView.pagingEnabled = YES;
myScrollView.delegate = self;
myImagesCount = 5;
myScrollView.showsHorizontalScrollIndicator=NO;
myScrollView.showsVerticalScrollIndicator=NO;
for (int i = 1; i <= myImagesCount; i++)
{
NSString *imageName = [NSString stringWithFormat:#"screen-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = myScrollView.frame.size.height;
NSLog(#"%d -----",self.view.frame.size.width);
rect.size.width = myScrollView.frame.size.width;
imageView.frame = rect;
imageView.tag = i;
[myScrollView addSubview:imageView];
[imageView release];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
button.backgroundColor = [UIColor blackColor];
[self.view addSubview:button];
[self layoutScrollImages];
[super viewDidLoad];
}
Based off your question, you'd like to add the UIButton as a subview to the UIImageView - I'm guessing in the for loop? Immediate problem I see is that the actual button is being generated outside of the for loop. I think your for loop is intended to look like this:
for (int i = 1; i <= myImagesCount; i++)
{
NSString *imageName = [NSString stringWithFormat:#"screen-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
button.backgroundColor = [UIColor blackColor];
CGRect rect = imageView.frame;
rect.size.height = myScrollView.frame.size.height;
NSLog(#"%d -----",self.view.frame.size.width);
rect.size.width = myScrollView.frame.size.width;
imageView.frame = rect;
imageView.tag = i;
[imageView addSubview:button];
[myScrollView addSubview:imageView];
[imageView release];
}
Try this
Declare button globally and set
[self layoutScrollImages];
[self.view bringSubViewToFront:button];

Resources