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
}
Related
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);
}
This is MyViewController.m
actionNames[0] = #"hoge";
actionNames[1] = #"piyo";
NSMutableArray *actionConts = [[NSMutableArray alloc] init];
for(int i=0; i<[actionNames count]; i++){
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(DEVICE_WIDTH/3 * i, 0, DEVICE_WIDTH/3, 45)];
// actionNames is NSMutableArray
[btn setTitle:actionNames[i] forState:UIControlStateNormal];
[btn setUserInteractionEnabled:YES];
actionConts[i] = btn;
}
// ↓ does not work
[actionConts[1] addTarget:self action:#selector(follow) forControlEvents:UIControlEventTouchUpInside];
// actionSubs is NSMutableArray
for(int i=0; i<[actionConts count]/3; i++){
actionSubs[i] = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 45)];;
}
for(int i=0; i<[actionConts count]; i++){
int sub = floor( (float)i / 3.0 );
[actionSubs[sub] addSubview:actionConts[i]];
}
UIScrollView *actionScr = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 150, 320, 45)];
[actionScr setPagingEnabled:YES];
[actionScr setShowsHorizontalScrollIndicator:NO];
[actionScr setBounces:NO];
actionScr.contentSize = CGSizeMake([actionSubs count] * DEVICE_WIDTH, 45);
for(int i=0; i<[actionSubs count]; i++){
[actionScr addSubview:actionSubs[i]];
}
Create a button. => actionConts[n]
addTarget to actionConts[1]
actionSubs[] alloc
addSubView actionConts[n] to actionSubs[n]
addSubView actionSubs[n] to actionScr
(addSubView actionScr to drw)
(drw is a UIView instance. "IBOutlet UIView *drw;")
When I click this button, It does not work... (´;ω;`)
Please help me.
Thanks.
Your code works fine to me. I only replaced some lines:
[actionConts[1] addTarget:self action:#selector(follow) forControlEvents:UIControlEventTouchUpInside];
replaced with:
for (UIButton *button in actionConts) {
[button addTarget:self action:#selector(follow) forControlEvents:UIControlEventTouchUpInside];
}
that allows adding the actions to each button from array.
Also I replaced for-loop condition from:
for(int i=0; i<[actionConts count]/3; i++)
to:
for(int i=0; i<ceil((float)[actionConts count]/3.0); i++)
that allows having at least one superview for buttons.
Now follow is a method that will fire for each your button. You can set UIButton.tag property to clarify which one is pressed.
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];
}
I need to change the pictures into animation video, but I don't known how to make a video, do not know to have what good method?
for example make your image names as xxxx-1.png, xxxx-2.png ...... xxxx-80.png. and paste the below code.. then the images will animate like video.
NSMutableArray* imgArray = [[NSMutableArray alloc] init];
for (int i=1; i <= 80; i++) {
[imgArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"xxxx-%d.png",i]]];
}
UIImageView* imgView = [UIImageView alloc] initWithFrame:(x,y,a,b)];
imgView.animationImages = imgArray;
imgView.animationDuration = 0.25;
imgView.animationRepeatCount = 0;
[imgView startAnimating];
[self.view addSubview:imgView];
[imgView release];