Objective-C: Setting an alpha value with an array of elements - ios

I'm quite new at objective-c and I'm trying to set an array of labels at 0.0 Alpha with a for loop. Can someone give me a help?
NSArray *fadeLabels = #[#"_lonLabel", #"_firstLat",#"_firstLon",#"_firstReal",#"_firstMagnetic",#"_firstSpeed",#"_speedLabel",#"_realNorthLabel",#"_magneticNorthLabel"];
for (int i=0; i<[fadeLabels count]; i++) {
[fadeLabels objectAtIndex:i];
//setAlpha:0.0f?
}

Use an array of views instead of array of strings. insert you views in the array.
NSArray *fadeLabels = #[ _lonLabel, _firstLat, _firstLon, _firstReal, _firstMagnetic, _firstSpeed, _speedLabel, _realNorthLabel, _magneticNorthLabel];
for (int i=0; i<[fadeLabels count]; i++) {
UIView *view = (UIView *)[fadeLabels objectAtIndex:i];
[view setAlpha:0.0f];
}

Your are assigning array with the String with name is equalTo your UILabel object so you need to
replace your array elements like this
#"_lonLabel" to _lonLabel
NSArray *fadeLabels = #[ _lonLabel, _firstLat, _firstLon, _firstReal, _firstMagnetic, _firstSpeed, _speedLabel, _realNorthLabel, _magneticNorthLabel];
for (int i=0; i<[fadeLabels count]; i++) {
UILabel *label = (UILabel *)fadeLabels[i];
label.alpha = 0.0;
}

You also should better use for...in instead of the "old" for.
NSArray *fadeLabels = #[ _lonLabel, _firstLat, _firstLon, _firstReal, _firstMagnetic, _firstSpeed, _speedLabel, _realNorthLabel, _magneticNorthLabel];
for (UILabel *label in fadeLabels) {
label.alpha = 0.0;
}

Related

Is there a way to shuffle/rearrange buttons on an iOS application?

I am developing an iOS application. I have 35 buttons on the storyboard. Is there anyway I can use a random generator to randomly rearrange or shuffle the buttons and still maintain the button's functionality?
You can add all the button objects to an NSMutableArray called buttonsArray and then use the following loop to exchange the buttons:
for (int i = 0; i < buttonsArray.count; i++) {
int random1 = arc4random() % [buttonsArray count];
int random2 = arc4random() % [buttonsArray count];
[buttonsArray exchangeObjectAtIndex:random1 withObjectAtIndex:random2];
}
If each of the button is belongs to a class like below then its really easy to shuffle them,
MyRandomButton {
NSInteger uniqueId;
NSString *title;
....
....
}
Now, what you can do is, at initial, prepare number of objets of MyRandomButton (equals to count of buttons on your StoryBoard). And add it into a mutable array (NSMutableArray).
Like this,
for(NSInteger index = 1; index <= 35; index++) {
MyRandomButton *btnInfo = ...;
btnInfo.uniqueId = index;
btnInfo.title = #sometitle;
[myButtonsArray addObject:btnInfo];
}
In next step, if you're done with something (and you want to shuffle all buttons) then get help from this question, What's the Best Way to Shuffle an NSMutableArray? to shuffle your array.
Loop through all buttons on your StoryBoard (in your case: 1 to 35).
for(NSInteger index = 1; index <= 35; index++) {
UIButton *button = (UIButton *)[yourView viewWithTag:index];
MyRandomButton *btnInfo = [myButtonsArray objectAtIndex:(index-1)];
button.tag = btnInfo.uniqueId;
[button setTitle:btnInfo.title forState:UIControlStateNormal];
}
Inside above loop, we've updated buttons title and tag.
Done!
That is possible, it will require some coding:
Create NSMutableArray with all buttons of the view in it.
Randomize its order.
Run over the buttons array and on each iteration keep last
button view and switch its rect with current button and
vice versa.
For better understanding, here is the code (ready for use, just copy paste):
- (void)viewDidLoad {
[super viewDidLoad];
// Getting buttons into array
NSMutableArray * buttonsArr = [self getButtonsArray];
// Shuffling buttons location
if(buttonsArr && [buttonsArr count]) {
[self randomizeButtonsLocation:buttonsArr];
} else {
NSLog(#"No buttons found");
}
}
- (void)randomizeButtonsLocation:(NSMutableArray *)buttonsArr {
// Randomizing order in button array
buttonsArr = [self randomizeArr:buttonsArr];
UIButton * lastBtn = nil;
for (UIButton * btn in buttonsArr) {
if(!lastBtn) {
lastBtn = btn;
} else {
// Put a side frames of last and current buttons
CGRect lastBtnRect = [lastBtn frame];
CGRect currentBtnRect = [btn frame];
// Switch frames
[btn setFrame:lastBtnRect];
[lastBtn setFrame:currentBtnRect];
// Keeping last btn for next iteration
lastBtn = btn;
}
}
}
- (NSMutableArray *)getButtonsArray {
// Getting all buttons in self.view and inserting them into a mutable array
NSMutableArray * resultArr = [[NSMutableArray alloc] init];
for (UIButton * btn in [self.view subviews]) {
if([btn isKindOfClass:[UIButton class]]) {
[resultArr addObject:btn];
}
}
return resultArr;
}
- (NSMutableArray *)randomizeArr:(NSMutableArray *)arr {
NSUInteger count = [arr count];
for (NSUInteger i = 0; i < count; ++i) {
unsigned long nElements = count - i;
unsigned long n = (arc4random() % nElements) + i;
[arr exchangeObjectAtIndex:i withObjectAtIndex:n];
}
return arr;
}
For anyone who needs to do this in SWIFT, here's how:
func randomlyPositionAnswerButtons() {
var posX = CGFloat()
var posY_button1 = CGFloat()
var posY_button2 = CGFloat()
var posY_button3 = CGFloat()
// if self.view.frame.size.height == 667 { /* ======== IPHONE 7, 7S, 8 ======== */
posX = 24 // Set all buttons to start from 37
posY_button1 = 310
posY_button2 = 405
posY_button3 = 500
// }
let tab = [
[1,2], [0,2], [0,1]
]
let indexArray = [
NSValue(cgRect:CGRect(x: posX, y: posY_button1, width: 350, height: 74)),
NSValue(cgRect:CGRect(x: posX, y: posY_button2, width: 350, height: 74)),
NSValue(cgRect:CGRect(x: posX, y: posY_button3, width: 350, height: 74))
] as NSMutableArray
// Shuffle the array
var p = UInt32()
p = arc4random() % 3
indexArray.exchangeObject(at: 0, withObjectAt: Int(p))
indexArray.exchangeObject(at: tab[Int(p)][0], withObjectAt: tab[Int(p)][1])
// Assign the buttons their new position
optionButtonA.frame = (indexArray[0] as AnyObject).cgRectValue
optionButtonB.frame = (indexArray[1] as AnyObject).cgRectValue
optionButtonC.frame = (indexArray[2] as AnyObject).cgRectValue
}
Rearranging the fixed buttons sounds like a headache, if only in terms of autolayout constraints.
Why don't you:
Create and position 35 "generic" buttons on the storyboard, each with their own outlet (properties named button01, button02, etc.), or better: use an outlet collection?
Assign a unique tag to each button (0, 1, 2, 3...) on the storyboard.
Assign the same #IBAction method to all your buttons on the storyboard.
At runtime, determine and assign a specific "role" to each button at random (and set each button's label accordingly). But keep track of which button is which, using -for example- a dictionary.
The action linked to all buttons will use the tag of the sender and the information contained in the dictionary mentioned above to determine which "role" button was tapped, and act accordingly.
Does it make sense?

Displaying UILabel dynamcially from the NSMutableArray

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.

Randomly swap the positions of 4 UIButtons

I have 4 UIButtons, positioned in my VC. I want to randomly swap the positions of each button. I've placed all of the UIButton positions into a NSMutableArray
self.frameArray = [[NSMutableArray alloc] init];
[self.frameArray addObject:[NSValue valueWithCGRect:CGRectMake(60,171,64,64)]];
[self.frameArray addObject:[NSValue valueWithCGRect:CGRectMake(198,171,64,64)]];
[self.frameArray addObject:[NSValue valueWithCGRect:CGRectMake(60,333,64,64)]];
[self.frameArray addObject:[NSValue valueWithCGRect:CGRectMake(198,333,64,64)]];
`
and all of my buttons are in a NSArray
self.buttons = #[self.button1, self.button2, self.button3, self.button4];
but i'm really not sure where to go from here, to make the positions of the UIButtons to randomly swap. Any help would be appreciated
Basically you want to create the frameArray then randomize the order of the frames and then apply them to the buttons.
Step One: Randomize the frameArray (place code after creation of frameArray)
NSUInteger count = [self.frameArray count];
for (NSUInteger i = 0; i < count; ++i) {
NSInteger remainingCount = count - i;
NSInteger exchangeIndex = i + arc4random_uniform((u_int32_t )remainingCount);
[self.frameArray exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex];
}
Step Two: Set Button Frames
for (int i = 0; i < [self.buttons count]; i++) {
[[self.buttons objectAtIndex:i] setFrame:[self.frameArray objectAtIndex:i]];
}
You can use arc4random_uniform() to get a random button and a random Rect from the arrays, and then assign the random Rect to the random button's frame:
NSInteger randomButton = arc4random_uniform((u_int32_t)self.buttons.count);
NSInteger randomRect = arc4random_uniform((u_int32_t)self.frameArray.count);
UIButton *button = self.buttons[randomButton];
NSValue *theRectValue = self.frameArray[randomRect];
CGRect rect = [theRectValue CGRectValue];
button.frame = rect;
I think this will work.

Loop through UIImageViews - Objective C

I have some UIImageViews in an array and want to loop through them to set an image, but I don't know how.
If I have one ImageView, imgv, then I do this:
self.imgv.image =
But how do I behave with many UIImageViews in a loop? I can't do the following:
for(int i = 0; i < [imageViewArray count]; i++)
{
self.i.image =
}
You use the index in the loop to get an item out of the array:
for(int i = 0; i < [imageViewArray count]; i++)
{
UIImageView *imageView = imageViewArray[i];
imageView.image = ...;
}
or, you use a for in loop:
for (UIImageView *imageView in imageViewArray)
{
imageView.image = ...;
}
for(int i = 0; i < [imageViewArray count]; i++)
{
UIImageView *tempImageView = [imageViewArray objectAtIndex:i];
tempImageView.image=image;
}
for(int i = 0; i < [imageViewArray count]; i++)
{
[imageViewArray[i] setImage:image];
}
If you need to set same image to all image views in the array then simply use,
[imageViewArray makeObjectsPerformSelector:#selector(setImage:) withObject:image];
If you need to set different images then you have to loop through the array like most of the answer have mentioned.
Hope that helps!
Try this
for(UIImageView *imgVw in imageViewArray)
{
imgVw.image = ...;
}
for - in is used to iterate through all the element of an array.

Is there a way to make a UICollectionView row or header stay still a.k.a. freeze panes?

I'm wondering if there is a way to make a UICollectionView row or header stay still similar to the freeze panes function in a spreadsheet program.
I basically want the first column and row to stay still while the rest is pannable.
Is this possible to do with the UICollectionView?
I have found that the following code will set the first row/section as always visable and will scroll with the appropriate section/row:
- (NSArray *) layoutAttributesForElementsInRect: (CGRect) rect
{
NSMutableArray *attributes = [NSMutableArray array];
for (NSInteger section = 0; section < self.collectionView.numberOfSections; section++)
for (NSInteger item = 0 ; item < [self.collectionView numberOfItemsInSection: section]; item++)
{
UICollectionViewLayoutAttributes *layout = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:item inSection:section]];
if(section==0 || item==0)
{
UICollectionView * const cv = self.collectionView;
CGPoint const contentOffset = cv.contentOffset;
CGPoint origin = layout.frame.origin;
if(item==0)
{
origin.x = contentOffset.x;
layout.zIndex = 1022;
}
if(section==0)
{
origin.y = contentOffset.y;
layout.zIndex = 1023;
if(item==0)layout.zIndex = 1024;
}
layout.frame = (CGRect)
{
.origin = origin,
.size = layout.frame.size
};
}
[attributes addObject:layout];
}
return attributes;
}

Resources