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.
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])];
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);
}
I want to load set of objects coming from array in vertical scrollview. I have UIScrollview at xib. Programmatically I added photo image, item name, item tags, comment button and likes label to UIView and added UIView to UIScrollview.
I added likes button to UIScrollview but the button action is not firing. Below is my code.
I think some Frames of subviews are over riding that makes button action (likes_ButtonPressed) not fired.
-(void)createScrollview
{
NSArray *viewsToRemove = [scrollview subviews];
for (UIView *v in viewsToRemove) [v removeFromSuperview];
int x=7;
int y=0;
CGRect screenbounds=[[UIScreen mainScreen] bounds];
for(int i=0;i<[array count];i++)
{
ProfileObjCls *profileObjRef = [self.array objectAtIndex:i];
//NSLog(#"Button Tag is %#",profileObjRef.image_UserLoaded_objInArray);
UIView *mainView=[[UIView alloc]initWithFrame:CGRectMake(x,y,310,400)];
self.photoTagImgView=[[UIImageView alloc]initWithFrame:CGRectMake(x,y+20,294,320)];
self.likesLabel.font=[UIFont fontWithName:#"BodoniStd-BoldItalic" size:13];
self.itemNameLbl=[[UILabel alloc]initWithFrame:CGRectMake(x,y+340,100,30)];
commentsButton=[[UIButton alloc]initWithFrame:CGRectMake(x+155+32, y+340, 40, 40)];
[commentsButton setImage:[UIImage imageNamed:#"comment_icon.png"] forState:UIControlStateNormal];
UIButton *photoLikeButton=[[UIButton alloc]initWithFrame:CGRectMake(x+100, y+340, 40, 40)];
photoLikeButton.tag=i;
[photoLikeButton setImage:[UIImage imageNamed:#"like_img_new.png"] forState:UIControlStateNormal];
[photoLikeButton addTarget:self action:#selector(likes_ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.likesLabel=[[UILabel alloc]initWithFrame:CGRectMake(x+223+32,y+346,310,30)];
if (profileObjRef.photoLikes==0)
{
self.likesLabel.text=#"0 likes";
}
else
{
self.likesLabel.text=[NSString stringWithFormat:#"%# likes",profileObjRef.photoLikes];
}
[self.photoTagImgView setImageWithURL:[NSURL URLWithString:profileObjRef.image_UserLoaded_objInArray]
placeholderImage:[UIImage imageNamed:#"satisfashionplaceholder#568.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
{
if (!error && image)
{
}
}];
self.imagetotweet=profileObjRef.image_UserLoaded_objInArray;
//int y=335;
//first label - PHOTO NAME
self.itemNameLbl.text=profileObjRef.photoname;
self.itemNameLbl.font = [UIFont fontWithName:#"BodoniStd-Poster" size:14.0];//BodoniStd-Bold
self.itemNameLbl.textColor = [UIColor blackColor];
//second Label - PHOTO DESCRIPTION
UIFont *cellFont=[UIFont fontWithName:#"BodoniStd-BoldItalic" size:13.0];
[mainView addSubview:commentsButton];
[mainView addSubview:likesLabel];
[self.scrollview addSubview:photoLikeButton];
[self.scrollview bringSubviewToFront:photoLikeButton];
[mainView addSubview:self.photoTagImgView];
[mainView addSubview:self.itemNameLbl];
[mainView addSubview:self.detailsBgImgView];
[self.scrollview addSubview:likesButton];
[self.scrollview bringSubviewToFront:likesButton];
mainView.userInteractionEnabled=YES;
self.scrollview.backgroundColor=[UIColor clearColor];
self.scrollview.userInteractionEnabled = YES;
/*
self.scrollview.exclusiveTouch = YES;
self.scrollview.canCancelContentTouches = YES;
self.scrollview.delaysContentTouches = YES;
self.view.userInteractionEnabled=YES;*/
//self.scrollview.backgroundColor=[UIColor colorWithRed:212.0f/255.0f green:213.0f/255.0f blue:214.0f/255.0f alpha:1.0f];
[self.scrollview addSubview:mainView];
[self.view addSubview:self.scrollview];
y=y+200;
//x=x+320;
}
self.scrollview.contentSize=CGSizeMake(320, 400*array.count);
//self.scrollview.contentSize=(CGSizeMake(x, 320));
self.scrollview.pagingEnabled=YES;
//[self.scrollview setContentOffset:CGPointMake(320*tagValue, 0)];
[self.scrollview setContentOffset:CGPointMake(0, 400*tagValue)];
[self.view bringSubviewToFront:spinner];
//[self.scrollview setContentOffset:CGPointMake(320*(tagValue-1), 0)];
}
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return ![view isKindOfClass:[UIButton class]];
}
Please suggest any ideas where I went wrong..
Thanks in Advance..
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 images in a UIScrollView, but the images are not displaying properly. The problem is that images are not at the same size. I want every image with the same size and height.
for (UIView *v in [_scrollView subviews]) {
[v removeFromSuperview];
}
//CGRect workingFrame = _scrollView.frame;
CGRect workingFrame = CGRectMake(0, 0, 200, 134);
workingFrame.origin.x =0;
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
for(NSDictionary *dict in info) {
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[_scrollView addSubview:imageview];
[imageview release];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
}
self.chosenImages = images;
NSLog(#"values=%#",_chosenImages);
[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
If you set UIViewContentModeScaleAspectFit you will have the image with the maximum size of the UIImageView, but often smaller. If you want it to fill and have all the same size and keep aspect you have to set:
[imageview setContentMode:UIViewContentModeScaleAspectFill];
[imageview setClipsToBounds:YES];