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);
}
Related
I have an UIImageView and I want to implement fling feature, when users flings on the screen the page can go up or down quickly.
Now my thinks is add two UISwipeGesturesRecognizers, but I don't know how to do next, should I use an animation to do this, and how to calculate the animation distance and time?
Also I find other answers said no need gesture recognizer can use scroll view, I am totally confused, are there any sample I can learn?
You can do it like that :
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setPagingEnabled:YES];
[scrollView setAlwaysBounceVertical:YES];
NSArray *imagesArray = [NSArray arrayWithObjects:#"img1.png", #"img2.png", #"img3.png", nil];
for (int i = 0; i < [imagesArray count]; i++)
{
CGFloat xOrigin = i * scrollView.frame.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
[imageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[scrollView addSubview:imageView];
}
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height*[imagesArray count])];
I am fetching an array of images that I have stored in PARSE and it all seems to be working fine, except that I am getting a blinking black flashing screen before the UIScrollView is visible. I believe that the black flash is this line:
[carousel setBackgroundColor:[UIColor blackColor]];
The background of the UIScrollView seems to be shown every time that I am loading a new image onto the carousel. I suspect that this is the cause of the blinking black screen.
NOTE This code is inside of a PFTableViewCell
Here is my code:
[PFObject fetchAllIfNeededInBackground:[gallery objectForKey:kFTPostPostsKey] block:^(NSArray *objects, NSError *error) {
if (!error) {
carousel = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320.0f,320.0f)];
[carousel setUserInteractionEnabled:YES];
[carousel setDelaysContentTouches:YES];
[carousel setExclusiveTouch:YES];
[carousel setCanCancelContentTouches:YES];
[carousel setBackgroundColor:[UIColor blackColor]];
//add the scrollview to the view
carousel.pagingEnabled = YES;
[carousel setAlwaysBounceVertical:NO];
int i = 0;
for (PFObject *post in objects) {
PFFile *file = [post objectForKey:kFTPostImageKey];
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTapImageInGalleryAction:)];
singleTap.numberOfTapsRequired = 1;
CGFloat xOrigin = i * 320;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, 320.0f, 320.0f)];
[imageView setBackgroundColor:[UIColor blackColor]];
UIImage *image = [UIImage imageWithData:data];
[imageView setImage:image];
[imageView setClipsToBounds:YES];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[imageView setUserInteractionEnabled:YES];
[imageView addGestureRecognizer:singleTap];
[carousel addSubview:imageView];
}
}];
i++;
}
[carousel setContentSize: CGSizeMake(320.0f * objects.count, 320.0f)];
[self.galleryButton addSubview:carousel];
[self.imageView setHidden:NO];
}
}];
My question is what is causing this blinking black screen? And how can I fix it?
Thanks!
I ended up moving this piece of code out of my the method which was being called often:
carousel = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320.0f,320.0f)];
[carousel setUserInteractionEnabled:YES];
[carousel setDelaysContentTouches:YES];
[carousel setExclusiveTouch:YES];
[carousel setCanCancelContentTouches:YES];
[carousel setBackgroundColor:[UIColor blackColor]];
//add the scrollview to the view
carousel.pagingEnabled = YES;
[carousel setAlwaysBounceVertical:NO];
I moved that into the init and that did the trick for me. Now I just initialize the UIScrollView once and remove/add subviews when I need them refreshed.
i'm creating a app where i'm adding some UIImages as subview of UIview. Now if I delete image I want to rearrange the remaining UIImages.
How can I achieve this?
EDIT
This is what i have tried so far:
for (int i=0; i<[array count]; i++)
{
id dict = [array objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100*i+100, 60, 60)];
[imageView setBackgroundColor:[UIColor clearColor]];
[imageView setTag:i+1];
[ImagesArray addObject:imageView];
[self.view addSubView: imageView];
}
Add You all Images in NSMutableArray and declare NSMutableArray in .h file.
self.imagArray = [[NSMutableArray alloc] init];
[self.imagArray addObject:[UIImage imageWithName:#"image1.png"];
.
.
.
.
[self.imagArray addObject:[UIImage imageWithName:#"image_N.png"];
And you can easily manage it for add/delete image by self.imagArray.
If you want to delete any image then also write this code
[self.imagArray removeObjectsAtIndexes:indexes];
I assume you are using array of images. Just remove the index once it's deleted and have a login to refresh the view.
Init images to array first:
for (int i=0; i<[array count]; i++)
{
id dict = [array objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100*i+100, 60, 60)];
[imageView setBackgroundColor:[UIColor clearColor]];
[ImagesArray addObject:imageView];
[self.view addSubView: imageView];
}
Delete image:
NSUInteger deleteIdx = [ImagesArray indexOfObject:deleteImage];
[deleteImage removeFromSuperview];
[self relayoutViews:deleteIdx];
- (void)relayoutViews:(int)deleteIdx{
for (int i=deleteIdx; i<[ImagesArray count]; i++) {
UIImageView *imageView = [ImagesArray objectAtIndex:i];
imageView.frame = CGRectMake(20, 100*i+100, 60, 60)];
}
}
With the help of tag you can get the imageview which user want to delete .So you have to add TapGesture on UIimageView and you have to bind method for deleting image.Like this
for (int i=0; i<[array count]; i++)
{
id dict = [array objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100*i+100, 60, 60)];
[imageView setBackgroundColor:[UIColor clearColor]];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(oneTap:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[imageView addGestureRecognizer:singleTap];
[imageView setTag:i+1];
[ImagesArray addObject:imageView];
[self.view addSubView: imageView];
}
- (void)oneTap:(UIGestureRecognizer *)gesture {
int myViewTag = gesture.view.tag;
// Now with the help of tag you can remove object from you array and which you are want to remove and then you can reload you view
}
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.
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];
}