Just wondering how i can dynamically name Uiviews or variables in general and then reference them in Obj C(X code), and once created the best way to reference them.
I read somewhere else that you should define the list in an array and then create, but the problem is that my users number is dynamic.
for(int x = 0; x <= 10; x++)
{
UIView *"User_"&(x) = [[UIView Alloc] init];
User_x.frame = CGRectMake( 10 * x , 10 , 10 ,10);
}
What is the best way to approach doing something like this?
If your project has ARC=Yes, the compiler will delete reference foreach cycle of your view.
However you can add a tag on your views:
for(int x = 0; x <= 10; x++)
{
UIView *user = [[UIView Alloc] init];
user.tag=x;
User.frame = CGRectMake( 10 * x , 10 , 10 ,10);
[yourSuperView addSubview:user];
}
It can be done like this
NSMutableArray *viewArr= [#[] mutableCopy];
for(int x = 0; x < 10; x++)
{
UIView *view = [[UIView Alloc] init];
view.frame = CGRectMake( 10 * x , 10 , 10 ,10);
[viewArr addObject:view];
}
Related
I have a mutable array as below
labelArrays = [NSMutableArray arrayWithObjects:#"One”, #“Two”, #“Three", nil];
I want to display all the list array values as individual label. This is like displaying the UILabel dynamically using the array values. What i did is as below,
int count=20;
for(int i = 0; i < [labelArrays count]; i++){
label1 = [[UILabel alloc]init];
label1.text = [labelArrays componentsJoinedByString:#" "];
label1.textColor = [UIColor colorWithRed:0.643 green:0.643 blue:0.639 alpha:1];
label1.layer.borderWidth = 1.0;
label1.layer.cornerRadius = 8;
count+=20;
[self addSubview:label1];
}
Here I can only display all the array values inside the single label. How can i display the array values in multiple labels dynamically.
//viewcontroller
myView.label1.frame =CGRectMake(588,200,200,28);
your coding is fine , but you are not added the frame , try this
label1 = [[UILabel alloc] initWithFrame: CGRectMake(0,count,aslikeyourwidth,aslikeyourheight)];
Define a public method for view in which you are willing to add the UILabel, pass a CGRect to that method and then according to your design, with each iteration update the x coordinate or y coordinate. Follow the below steps:
// code in your viewcontroller
[myView designYourMultipleLabelsWithStartingFrame:CGRectMake(588,200,200,28)]; // pass the frame of the first label.
Now add the multiple UILabel in your view class, let's assume you have set labelArrays.
// code in view class
-(void) designYourMultipleLabelsWithStartingFrame:(CGRect) frame{
float xCoordinate = frame.origin.x;
float yCoordinate = frame.origin.y;
for(int counter = 0; counter < [labelArrays count]; counter++){
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(xCoordinate,yCoordinate,labelWidth,labelHeight)];
label.text = [labelArrays objectAtIndex:counter];
[self addSubview:label];
// update the x coordinate or y coordinate according to your design. Let's say we need the labels in vertical order, so update the y Coordinate by labelHeight and gap between two labels.
yCoordinate = yCoordinate + labelHeight + gapBetweenTwoLabels.
}
}
Hope this will help.
Just a thought - would you not be better using a tableview to show the array?
It looks like you may need to just handle their position on the view as the code looks like it is adding the labels but it is placing them over each other.
So i need to add programatically 3 images using a for loop this is my code , it's not correct, i just tryed.
for(int i = 1; i < 4; i++){
UIImageView *[i] = [[UIImageView alloc] initWithFrame:CGRectMake([i]*50, 50, 250, 250)];
[self.view addSubview:[i]];
}
where [i]; will be 1 , 2 and 3 any help would be appreciated. Thanks
You syntax is wrong. This is more what you need.
for (int i=0;i<3;i++){
UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(i*50, 50, 250, 250)];
[self.view addSubView:image];
}
However, this is not much use to you as your images are not referenced from anywhere so you can not populate or adjust them easily.
Change the variable name from [i] to some name like imageToAdd.
CGFloat xMarging = 0;
CGFloat imageOriginX = 0;
CGFloat imageOriginY = 0;
CGFloat imageWidth = 250;
CGFloat imageHeight = 250;
NSInteger numerOfImages = 3;
for (NSInteger i = 0; i < numerOfImages; i++){
[self.view addSubview:[[UIImageView alloc] initWithFrame:CGRectMake(xMarging + i * imageWidth,
imageOriginY,
imageWidth,
imgageHeight)]];
}
assuming you want them side by side as in [][][]
I'am searching for a (very) simple way to crossfade images within animationDuration time. I tried using NSTimer and some other stuff I found here, but no solution satisfy or helps me.
I create this stuff in a method (without comments) ...
- (UIImageView *)playSequence{
NSArray *structureArray = self.contenManager.ImageViewControllerSize;
NSMutableArray *structuresToPlay = [[NSMutableArray alloc] init];
// walk thru current collection
for(NSInteger i = 0; i < [self.currentCollection count]; i++)
{
NSInteger index = [[self.currentCollection objectAtIndex:i] integerValue];
[structuresToPlay addObject:[UIImage imageNamed:[structureArray objectAtIndex:index]]];
}
_playSequence = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
_playSequence.animationImages = structuresToPlay;
_playSequence.animationDuration = 5 * structuresToPlay.count;
[_playSequence startAnimating];
return _playSequence;
}
Here is the part where the method is called ...
- (void)justPlaySelection
{
UIView *justPlaySelection = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
// some background music code ...
// add play sequence
[justPlaySelection addSubview:self.playSequence];
// add close button
// { ... }
[self presentSemiView:justPlaySelection];
}
- (UIImageView *)playSequence
{
if (_playSequence == nil)
{
NSArray *structureArray = self.contenManager.ImageViewControllerSize;
NSMutableArray *structuresToPlay = [[NSMutableArray alloc] init];
// walk thru current collection
for(NSInteger i = 0; i < [self.currentCollection count]; i++)
{
NSInteger index = [[self.currentCollection objectAtIndex:i] integerValue];
[structuresToPlay addObject:[UIImage imageNamed:[structureArray objectAtIndex:index]]];
}
_playSequence = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
}
_playSequence.animationImages = structuresToPlay;
_playSequence.animationDuration = 5 * structuresToPlay.count;
[_playSequence startAnimating];
return _playSequence;
}
Here is another way to switch between to UIImageViews, it wouldn't be hard to add in the fade in and out.
- (void) performAnimationOfFrameAtIndex:(NSNumber*)indexNum
{
int index = indexNum.intValue;
BOOL completedSequece = index >= [self.frames count];
if (completedSequece) return;
UIImageView *imgIn;
UIImageView *imgOut;
if (index % 2 == 0) {
imgIn = self.animationImageViewOne;
imgOut = self.animationImageViewTwo;
}
else {
imgIn = self.animationImageViewTwo;
imgOut = self.animationImageViewOne;
}
imgIn.image = [self.frames objectAtIndex:index];
[self.view sendSubviewToBack:imgOut];
double speed = 0.1;
[self performSelector:#selector(performAnimationOfFrameAtIndex:) withObject:[NSNumber numberWithInt:index + 1] afterDelay:speed];
}
I created a project that does exactly what you describe (an effect like UIImageView animation sequence, but with a cross-fade between images.)
The project is on github: https://github.com/DuncanMC/Animate-Img
You need to have 2 image views.
The trick I use is to stack them on top of each other, and simply animate the top image view from alpha 1 -> 0, then switch images in the top image view and animate it from alpha 0 -> 1.0, so the new image is revealed.
The project demos a morph transition using only 5 frames. Each frame is marked with a number so you can see the frame changes. It has a switch which lets you turn cross-fading on or off.
in my app i'm using a scroll View (paging). each view in the scroll view (UIImageView) contains an image and each image has tags (buttons) scattered all over the pic. so here is my code:
for (int i = 0; i < self.pictures.count; i++)
{
//The first loop is to loop to every picture
int nbOfTags = classObject.tagsImages.count;
for (int j = 0; j < nbOfTags; j++)
{
//Second loop is to loop to each tag corresponding to the picture
ListTags *listTags = [[ListTags alloc]init];
NSMutableArray *tagInfo = [[NSMutableArray alloc]init];
listTags = [tagInfo objectAtIndex:j];
float tagX = [listTags.tag_x floatValue];
float tagY = [listTags.tag_y floatValue];
float x = 1024*i+ tagX;
float y = tagY;
CGRect rect = CGRectMake(x, tagY, 20, 20);
UIButton *button = [[UIButton alloc] initWithFrame:rect];
UIImage * buttonImage = [UIImage imageNamed:#"blueCircle.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.scrollView addSubview:button];
}
}
problem: when i run the code i can't see the buttons (tags) on the picture. if i replace "i" with "j" in
float x = 1024*i+ tagX;
the buttons can be seen, however it's not the desired coordinates. so why "i" can't work? am i doing anything wrong or missing anything?
In interface builder (or storyboard - IB) uncheck autolayout (if it's checked) then your button will be displayed where you want.
See my comments in the code below:
for (int i = 0; i < self.pictures.count; i++)
{
//The first loop is to loop to every picture
/*
* This will give you the same number of tags for every image.
* Is that what you want?
*/
int nbOfTags = classObject.tagsImages.count;
for (int j = 0; j < nbOfTags; j++)
{
//Second loop is to loop to each tag corresponding to the picture
/* This allocates a brand new ListTags object EVERY time through the loop. */
ListTags *listTags = [[ListTags alloc]init];
/* This allocates a brand new EMPTY array EVERY time through the loop. */
NSMutableArray *tagInfo = [[NSMutableArray alloc]init];
/*
* This discards the new ListTags object that you just created two lines ago
* and replaces it with an object from tagInfo. Which doesn't exist because
* you just created a new array in the previous line. This really should
* crash, which leads me to believe that this isn't the actual code that you
* are using. Post the actual code that you are using so that we can help you
* better.
*/
listTags = [tagInfo objectAtIndex:j];
/* None of this is valid since listTags is nil/we never should have gotten here. */
float tagX = [listTags.tag_x floatValue];
float tagY = [listTags.tag_y floatValue];
float x = 1024*i+ tagX;
float y = tagY;
CGRect rect = CGRectMake(x, tagY, 20, 20);
UIButton *button = [[UIButton alloc] initWithFrame:rect];
UIImage * buttonImage = [UIImage imageNamed:#"blueCircle.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.scrollView addSubview:button];
}
}
I have to do a pdf generator with custom label, so in my storyboard I have a UIScrollView with a UIView inside it, and if my UIView is full, I want to add another UIView and fill it with other label that is not already in. How can I add another page in my UIScrollView with another UIView ? I have to do it programmatically or there is an option in storyboard for doing it ?
I tried this (I don't know if it works and I don't want to compile it 'cause I find it really really ugly) :
CGFloat placement = titleLabel.frame.size.height + kMarginLabel;
for(int i = 0; i < [contract.nameContract count]; i++)
{
if(placement > _contractPageView.frame.size.height)
{
CGFloat secondPlacement = 0;
_scrollView.contentSize = CGSizeMake(_contractPageView.frame.size.width * 2, _contractPageView.frame.size.height);
UIView *secondPage = [[UIView alloc] initWithFrame:_contractPageView.frame];
articleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0 + kMarginLabel, secondPlacement, kTitleHeight, kTitleWidth)];
articleLabel.text = [NSString stringWithFormat:#"Article %i - %#",i, [contract.nameContract objectAtIndex:i]];
[secondPage addSubview:articleLabel];
secondPlacement += articleLabel.frame.size.height;
clauseLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, secondPlacement + kMarginLabel, _contractPageView.frame.size.width, kSizeOfClause)];
clauseLabel.text = [contract.textContract objectAtIndex:i];
secondPlacement += clauseLabel.frame.size.height;
[secondPage addSubview:clauseLabel];
[_scrollView addSubview:secondPage];
if(secondPlacement > secondPage.frame.size.height)
{
CGFloat thirdPlacement = 0;
_scrollView.contentSize = CGSizeMake(_contractPageView.frame.size.width * 3, _contractPageView.frame.size.height);
UIView *thirdPage = [[UIView alloc] initWithFrame:_contractPageView.frame];
articleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0 + kMarginLabel, thirdPlacement, kTitleHeight, kTitleWidth)];
articleLabel.text = [NSString stringWithFormat:#"Article %i - %#",i, [contract.nameContract objectAtIndex:i]];
[thirdPage addSubview:articleLabel];
thirdPlacement += articleLabel.frame.size.height;
clauseLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, thirdPlacement + kMarginLabel, _contractPageView.frame.size.width, kSizeOfClause)];
clauseLabel.text = [contract.textContract objectAtIndex:i];
thirdPlacement += clauseLabel.frame.size.height;
[thirdPage addSubview:clauseLabel];
[_scrollView addSubview:thirdPage];
}
} else {
articleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, placement, kTitleHeight, kTitleWidth)];
articleLabel.text = [NSString stringWithFormat:#"Article %i - %#",i, [contract.nameContract objectAtIndex:i]];
[_contractPageView addSubview:articleLabel];
placement += articleLabel.frame.size.height;
clauseLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, placement + kMarginLabel, _contractPageView.frame.size.width, kSizeOfClause)];
clauseLabel.text = [contract.textContract objectAtIndex:i];
placement += clauseLabel.frame.size.height;
[_contractPageView addSubview:clauseLabel];
}
}
I don't know if it's the only method, and imagine if I have many and many many many object in my array, I have to create more and more page :/ I don't know how to do it :(
Thanks for your help.
I did some similar code for a generic core data editor for one of our projects. So as I see it you got two options:
Use a collection view design the prototype view as your page and hook it up to an array controller that manages your text. The text could be organised as an array of NSAttributedString that you could bind to a NSTextView (one for each page)
If you need more flexibility, i.e. pages of different sizes, different layouts etc.., you could program your own container that keep as properties an array of "Pages" (you would need to define this class) and whenever this array is changed recomputes the sizes and constraints by, for example by adding up the heights of every pages and sizing your content view appropriately.
Hope it helps