I have a UIImageView that when a function called I want to change to a different ("active") image and when another called change back to the image before. This is the code:
- (NavButton *)initWithFrame:(CGRect *)fr andImage:(UIImage *)img andActiveImage:(UIImage *)acImg {
NavButton *a = [[NavButton alloc] initWithFrame:*fr];
[a setBackgroundColor:[UIColor clearColor]];
UIImageView *aImg = [[UIImageView alloc] initWithFrame:CGRectMake(8.5, 8.5, 28, 28)];
aImg.tag = 13;
aImg.image = img;
self.orginalImage = img;
self.activeImage = acImg;
[a addSubview:aImg];
return a;
}
- (void)setIsActive:(NSNumber *)isActive {
self.active = isActive;
if ([isActive isEqualToValue:[NSNumber numberWithBool:NO]]) {
[self undoActive];
} else {
[self redoActive];
}
}
- (void)undoActive {
UIImageView *a = (UIImageView *)[self viewWithTag:13];
a.image = self.orginalImage;
}
- (void)redoActive {
UIImageView *a = (UIImageView *)[self viewWithTag:13];
a.image = self.activeImage;
}
When I call [btn setIsActive:[NSNumber numberWithBool:YES]]; or [btn setIsActive:[NSNumber numberWithBool:NO]]; both times it removes the image, but when I don't call either the image stays there. So, how do I make it so when I call them it changes the images of the button to the correct image?
Instead of repeatedly assigning image to imageview, you can assign two images to the image view: one to "image" property and other to "highlightedImage" property. When you want to switch between the images, set the Boolean property "highlighted" as YES or NO.
It will be easier just to do your check as:
if (![isActive boolValue]) {
Then, do some debugging, add some breakpoints and / or logging. Check what values are actually being received. Are the flags set correctly. Are the images set correctly. Is anything nil.
Related
Currently I have the image folder the paths is Resource/images and inside the images folder there are several images, I would like to create dark mode using the same image name but the image are different in dark mode style and normal style. example(icon in dark mode is white but in normal mode is black)
I would like to know if I create 2 folder inside Resource/images/imageOneFolder and Resource/images/imageTwoFolder, how can I access the specific path to get the image.
Thanks
first you need to organize your icons in a better way for example:
Resources/images/black/myicon_black.png
Resources/images/white/myicon_white.png
Having this level of organization resolve the problem is very easy, I provide you two ways to resolve using the next logic:
#interface MyStructObject : NSObject
#property (nonatomic) NSString *imageName, *imageExtension, *fullImageName;
#end
#interface MyViewController : UIViewController
#end
#implementation MyViewController
// For this short version your icons needs to be white or black
// With the next code you can change the color of your icon
// Example: If dark mode active you need icons with whiteColor
// if not dark mode you need icons with blackColor
- (void)didExecuteShortVersion
{
// Initial path document or bundle
NSString *resourcesPath = #"Resources/images";
// Getting objects from array or your own struct
MyStructObject *myItem = [MyStructObject new];
// The image full path
NSString *fullImagePath = [NSString stringWithFormat: #"%#/%#", resourcesPath, myItem.fullImageName];
// Image Object
UIImage *imageIcon = [UIImage imageWithContentsOfFile: fullImagePath];
// Image Container
UIImageView *myImageView = [[UIImageView alloc] initWithFrame: CGRectZero];
[myImageView setImage: imageIcon];
myImageView.image = [myImageView.image imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate];
if (#available(iOS 12.0, *)) {
if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
[myImageView setTintColor: [UIColor whiteColor]];
}
else {
[myImageView setTintColor: [UIColor blackColor]];
}
}
else {
// Dark mode is not available
[myImageView setTintColor: [UIColor blackColor]];
}
// Adding view
[self.view addSubview: myImageView];
}
#pragma mark - Life Cycle
- (void)viewDidLoad
{
// Initial path document or bundle
NSString *resourcesPath = #"Resources/images";
// The image full path
NSString *fullImagePath = nil;
// Getting objects from array or your own struct
MyStructObject *myItem = [MyStructObject new];
if (#available(iOS 12.0, *)) {
if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
NSLog(#"Dark Mode Active");
// Assuming your struct only has the first name of icon
// Example: myItem.imageName -> myicon
// Adding the last path of name to match icon _black -> myicon_black
// If you have different kind of extension you can improve
// the logic of this
fullImagePath = [NSString stringWithFormat: #"%#/black/%#_black.%#", resourcesPath, myItem.imageName, myItem.imageExtension];
}
else {
NSLog(#"White Mode Active");
fullImagePath = [NSString stringWithFormat: #"%#/white/%#_white.%#", resourcesPath, myItem.imageName, myItem.imageExtension];
}
} else {
// Dark mode is not available
fullImagePath = [NSString stringWithFormat: #"%#/default/%#_default.%#", resourcesPath, myItem.imageName, myItem.imageExtension];
}
if (fullImagePath != nil) {
UIImage *imageIcon = [UIImage imageWithContentsOfFile: fullImagePath];
// Change the CGRect
UIImageView *myImageView = [[UIImageView alloc] initWithFrame: CGRectZero];
[myImageView setImage: imageIcon];
// Add myImageView to the UIView
[self.view addSubview: myImageView];
}
}
#end
So, I have repeating views containing thumbnails, once a thumbnail is pressed the thumbnail ID is sent as the tag.
With this information I want to get the frame of the subview of that view;
- (IBAction)thumbPressed:(id)sender {
NSLog(#"Thumb %i pressed", (int)[sender tag]);
for (int i = 0; i < _thumbCounter; i++) {
if (i == [sender tag]) {
NSLog(#"thumb view %i", i);
//Up To HERE
break;
}
}
// [self moveToVideoControllerWithInfoID:[NSString stringWithFormat:#"%li", (long)[sender tag]]];
}
For the thumbnails to be drawn they're drawn in a random order retrieved by JSON.
The Button
UIButton *thumbButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[thumbButton addTarget:self
action:#selector(thumbPressed:)
forControlEvents:UIControlEventTouchUpInside];
thumbButton.frame = CGRectMake(0, 0, _thumbView.frame.size.width, _thumbView.frame.size.height);
thumbButton.tag = _thumbCounter;
[_thumbView addSubview:thumbButton];
The subview I want to get the frame of
NSString *string = [NSString stringWithFormat:#"***.jpg",Link];
NSURL * imageURL = [NSURL URLWithString:string];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(thumbGap, thumbGap, thumbHeight - 5, thumbHeight - 5)];
imageView.image = image;
[_thumbView addSubview:imageView];
Where the thumbnail shell is drawn
_thumbView = [[ThumbView alloc] initWithFrame:CGRectMake(0, margin, _scrollView.frame.size.width, thumbHeight)];
_thumbView.backgroundColor = [UIColor whiteColor];
_thumbView.thumbId = _thumbCounter;
[_scrollView addSubview:_thumbView];
_thumbview is a class of UIVIEW with the added thumbId
How once that button is pressed can I locate the imageView frame inside of _thumbview ( bare in mind there are multiple ).
First off, let's make life easier for you:
- (IBAction)thumbPressed:(UIButton*)sender {
NSLog(#"Thumb %i pressed", (int)[sender tag]);
ThumbView *thumbView = (ThumbView*)[sender superView];
for(UIView* subview in thumbView.subViews) {
if([subView iKindOfClass:[UIImageView class]]) {
//you got it here
}
}
}
You could shortcut the whole thing by making ThumbView have an imageView property as well.
I've tried to determine that this is what you are doing:
_scrollView
->_thumbView[0]
--->thumbButton
--->imageView
->_thumbView[1]
--->thumbButton
--->imageView
...
->_thumbView[N]
--->thumbButton
--->imageView
Assumed that you want the image view who is in the same thumbView as the button who is pressed.
Best Possible Solution (IMHO):
#Interface ThumbView()
#property (nonatomic,weak) UIImageView *imageView;
#end
AND:
(After making sure to first add, and THEN SET .imageView)
- (IBAction)thumbPressed:(UIButton*)sender {
NSLog(#"Thumb %i pressed", (int)[sender tag]);
UIImageView *imageView = ((ThumbView*)[sender superView]).imageView
}
for (UIView *i in _scrollView.subviews) {
if ([i isKindOfClass:[ThumbView class]]) {
ThumbView *tempThumb = (ThumbView *)i;
if (tempThumb.thumbId == (int)[sender tag]) {
for (UIView *y in tempThumb.subviews) {
if ([y isKindOfClass:[UIImageView class]]) {
UIImageView *tempIMG = (UIImageView *)y;
[self playVideo:[loadedInput objectForKey:#"link"] frame:tempIMG.frame andView:tempThumb];
}
}
}
}
}
I have 2 labels.
First label, named "label" is placed inside every view within the carousel. The string/text of the label is the view's index.
label.text = [[items1 objectAtIndex:index] stringValue];
I also have a second label (outside the carousel) named "outsideLabel".
I want the outsideLabel's string/text to be the view's index aswell (always the view being in front of the carousel).
outsideLabel.text = [[items1 objectAtIndex:index] stringValue];
Somehow I am doing it wrong and wonder how I shall code this in order to show the proper number in outsideLabel's string/text (always the view being in front). The code somewhat shows the correct numbers but get messed up when scrolling backwards in the carousel. The carouseltype is timeMachine.
My current code:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];
view.contentMode = UIViewContentModeCenter;
label = [[UILabel alloc] initWithFrame:view.bounds];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
if (carousel == carousel1)
{
CGRect test = CGRectMake(10, 10, 20, 20);
self.label.frame = test;
}
else {
CGRect test = CGRectMake(50, 40, 40, 40);
self.label.frame = test;
}
[view addSubview:label];
}
else
{
label = [[view subviews] lastObject];
}
if (carousel == carousel1)
{
//items in this array are numbers
outsideLabel.text = [[items1 objectAtIndex:index] stringValue];
label.text = [[items1 objectAtIndex:index] stringValue];
((UIImageView *)view).image = [UIImage imageNamed:[view1background objectAtIndex:index]];
}
else
{
//not relevant....
}
return view;
}
From the code you've provided, it looks like you're not initializing the outsideLabel in the right place. To be safe, you should initialize all your subviews inside the block where you are checking if the view is nil. Another safe convention is to assign tags to all your subviews, so that you can later retrieve them from views that are reused, as in the code below. For easy reference, and to avoid errors, I define constants for these tags at the top of my implementation file, like this:
#define INSIDE_LABEL_TAG 1
#define OUTSIDE_LABEL_TAG 2
This is much safer, since it doesn't depend on the structure of the views, as is the case with your code, where you get the last view:
label = [[view subviews] lastObject];
Try initializing outsideLabel inside that block, and use tags. The pattern used in initialization is identical to that used for the subviews of UITableView cells in a UITableViewDataSource delegate:
(UITableViewCell * _Nonnull)tableView:(UITableView * _Nonnull)tableView
cellForRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath
Here is some pseudo code that shows where I would use tags and initialize the outsideLabel:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
//create new view if no view is available for recycling
if (view == nil)
{
//Configure the view
...
/* Initialize views for all carousels */
//Initialize the insideLabel and set its tag
...
insideLabel.tag = INSIDE_LABEL_TAG;
//Initialize the outsideLabel and set its tag
...
outsideLabel.tag = OUTSIDE_LABEL_TAG;
if (carousel == carousel1)
{
//Do any carousel-specific configurations
}
//Add all subviews initialized in this block
[view addSubview:label];
[view addSubview:outsideLabel];
}
else
{
//Get the subviews from an existing view
insideLabel = (UILabel *)[view viewWithTag:INSIDE_LABEL_TAG];
outsideLabel = (UILabel *)[view viewWithTag:OUTSIDE_LABEL_TAG];
}
if (carousel == carousel1)
{
//Set the values for each subview
} else {
//Other carousels...
}
return view;
}
It looks to me like you want the "time machine" style carousel. I don't see your code setting the carousel type anywhere. Don't you need to set the carousel type?
I have this simple function where i sap rate the button animation and then start animation for every button but i dont know why only one button animate not others which clicked, please help me with this
- (IBAction)startAnimation:(UIButton *)button {
NSMutableArray* imagesArray = [[NSMutableArray alloc] init];
for (int images = 0; images < 15; images++) {
UIImage* buttonImage = [UIImage imageNamed:[NSString stringWithFormat:#"aaqqq00%02d.png", images]];
[imagesArray addObject:buttonImage];
}
NSArray* reversedAnim = [[imagesArray reverseObjectEnumerator] allObjects];
int buttonTag = button.tag;
animButton.adjustsImageWhenHighlighted = NO;
for (int images = 0; images < 15; images++) {
UIButton *animButton = (UIButton *)[self.view viewWithTag:buttonTag];
if (images <= buttonTag) {
animButton.imageView.animationImages = imagesArray;
[animButton setImage:
[UIImage imageNamed:#"aaqqq0014.png"] forState:UIControlStateNormal];
} else {
animButton.imageView.animationImages = reversedAnim;
[animButton setImage:
[UIImage imageNamed:#"aaqqq0000.png"] forState:UIControlStateSelected];
}
NSLog(#"%#", animButton.imageView.animationImages);
animButton.imageView.animationDuration = 1; //whatever you want (in seconds)
animButton.imageView.animationRepeatCount = 1;
[animButton.imageView startAnimating];
}
}
You're passing the button, so doing the tag lookup seem pointless and error prone (if there are multiple views with the same tag in the hierarchy).
Setting the buttons image view animation images (animButton.imageView.animationImages) and then after that setting a single image ([animButton setImage:...) is also pointless.
What does your log statement say about the animation images?
I am trying to check if a image of a subview is hidden or not, by clicking a button. The log does display but i cant get the hidden status of the image somehow.
Whats going wrong here? Hope u can help me!
Viewdidload:
SubSlide1Hoofdstuk3 *subslide1 = [[SubSlide1Hoofdstuk3 alloc] init];
CGRect frame = self.view.frame;
frame.origin.x = 0;
frame.origin.y = 0;
subslide1.view.frame = frame;
// This works finaly
UIImageView *zwart = subslide1.imageZwart;
[zwart setImage:[UIImage imageNamed:#"imageblack.jpg"]];
[subslide1.b1 addTarget:self action:#selector(switchImageZwart:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:subslide1.view];
The IBAction to check the image in subview is hidden:
-(IBAction)switchImageZwart:(id)sender
{
SubSlide1Hoofdstuk3 *switchactie = [[SubSlide1Hoofdstuk3 alloc] init];
UIImageView *wit = switchactie.imageWit;
UIImageView *zwart = switchactie.imageZwart;
if(zwart.hidden == YES) {
NSLog(#"Image black is hidden!");
} else if(wit.hidden == YES) {
NSLog(#"Image white is hidden!");
} else {
NSLog(#"Can't say... :(");
}
}
The problem here is that inside your -(IBAction)switchImageZwart:(id)sender method you create a new instance of SubSlide1Hoofdstuk3 and checking its properties (the UIImageViews) instead of checking the actual UIImageView objects you created on viewDidLoad:. What you want actually is to hold a reference to subslide1 and check that instead.
Ps. Since the button calling the check method is actually a subview of your subslide1, you could get a reference like:
SubSlide1Hoofdstuk3 *switchactie = [sender superView];
EDIT: An example on your actual code:
in your .h file:
#property(nonatomic, strong) SubSlide1Hoofdstuk3 *subslide1;
in your .m file:
#synthesize subslide1;
- (void)viewDidLoad
{
//...
self.subslide1 = [[SubSlide1Hoofdstuk3 alloc] init];
CGRect frame = self.view.frame;
frame.origin.x = 0;
frame.origin.y = 0;
self.subslide1.view.frame = frame;
// This works finaly
UIImageView *zwart = self.subslide1.imageZwart;
[zwart setImage:[UIImage imageNamed:#"imageblack.jpg"]];
[self.subslide1.b1 addTarget:self action:#selector(switchImageZwart:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:self.subslide1.view];
}
-(IBAction)switchImageZwart:(id)sender
{
SubSlide1Hoofdstuk3 *switchactie = self.subslide1;
UIImageView *wit = switchactie.imageWit;
UIImageView *zwart = switchactie.imageZwart;
if(zwart.hidden == YES) {
NSLog(#"Image black is hidden!");
} else if(wit.hidden == YES) {
NSLog(#"Image white is hidden!");
} else {
NSLog(#"Can't say... :(");
}
}