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... :(");
}
}
Related
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 am trying to move a UIImage, first button press creates the image and second press moves it.
The image only needs to exist upon pressing the button.
In the simulator it creates the button and places it, the second time it click just doesn't do anything.
This is my Code
- (IBAction) btn:(id)sender {
UIImageView *myImage = [[UIImageView alloc] init];
myImage.image = [UIImage imageNamed:#"keyframe"];
if (startUp == 1){
//Create Image and add to view
myImage.frame = CGRectMake(200, 300, 10, 10);
myImage.image = [UIImage imageNamed:#"keyframe"];
[self.view addSubview:myImage];
//Set startUp to 0 and output rect value
startUp = 0;
NSLog(#"currentFrame %#", NSStringFromCGRect(myImage.frame));
}else if (startUp == 0){
//Change position, size and log to debug
myImage.frame = CGRectMake(500,100 ,20, 20);
NSLog(#"newFrame %#", NSStringFromCGRect(myImage.frame));
}
}
How do you programmatically move a programmatically added UIimage?
I tried changing the center value but that doesn't work either.
Try something like this – tested and working sample:
#import "ViewController.h"
#interface ViewController () {
BOOL startUp;
UIImageView *myImage;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
startUp = YES;
}
- (IBAction)doWork:(id)sender {
if (startUp) {
UIImage *img = [UIImage imageNamed: #"keyframe"];
myImage = [[UIImageView alloc] initWithImage: img];
[myImage sizeToFit];
[myImage setCenter: CGPointMake(200, 300)];
[self.view addSubview: myImage];
startUp = NO;
} else {
[myImage setCenter: CGPointMake(400, 500)];
}
}
#end
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.
I'm trying to create a scrollview with an array of clickable UIImageView's. My goal is that when an ImageView is clicked, it returns which position in the array it occupies. The problem is that i don't know how to "catch" the position's number. How do I do that?
So far I have:
- (IBAction)respondToTapGesture:(UITapGestureRecognizer *)recognizer {
NSLog(#"%#",)//here is where i want to return the element's position.
}
-(void) preenchemenu {
[menu setContentSize:CGSizeMake(400, 91)];
int x=0;
imagensmenu=[NSArray arrayWithObjects:[UIImage imageNamed:#"teste2.tiff"],[UIImage imageNamed:#"teste2.tiff"], nil];
for (int i = 0; i <3; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x,0 , 90, 91)];
x=x+90;
imageView.image = [imagensmenu objectAtIndex:i];
imageView.tag = 1000+ i;
imageView.userInteractionEnabled = YES;
imageView.multipleTouchEnabled = YES;
UITapGestureRecognizer *tapRecognizermenu = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(respondToTapGesture:)];
tapRecognizermenu.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tapRecognizermenu];
[menu addSubview:imageView];
}
}
You can create an array to hold you imageViews and then find the position of your imageView in this array when it is tapped.
Add a property for this
#property (nonatomic, strong) NSMutableArray *imageViews;
initialise it in init
- (id)init...
{
self = [super init...
if (self) {
_imageViews = [NSMutableArray array];
}
return self;
}
Then amend your current method slightly to also add the imageViews to this array as well as a subview of the menu
[self.imageViews addObject:imageView];
[menu addSubview:imageView];
Then in your gesture recognizer call back you can do
- (void)respondToTapGesture:(id)sender;
{
UIView *view = [sender view];
NSLog(#"%d", [self.imageViews indexOfObject:view]);
}
Just find the index by
- (IBAction)respondToTapGesture:(UITapGestureRecognizer *)recognizer
{
UIView *view = recognizer.view;
NSLog(#"Index of image in array is %d", view.tag-1000);
}
I am working on a reader app. when you read one magazine, it will display first five pages and download the rest pages one by one. there is a scrollview to view the thumbnail image of pages. At the beginning, if the page needs downloading, the corresponding thumbnail view's alpha value is set to 0.5 (the thumbnail images are in the file,no need to download). when the page is downloaded, i will update the thumbnail view's value to 1.0. I use one operation to download the page, and when one is downloaded i use delegate to set thumbnail view's alpha.
But when i update thumbnail view's alpha value, it still the same as the beginning. it seems the alpha has no effect. I wonder is there anything wrong with my code? some snippets are as follows:
In the PageViewController.m
- (void)loadView
{
[super loadView];
//...
[self createSlideUpViewIfNecessary];
[self downloadPages];
}
- (void)createSlideUpViewIfNecessary {
if (!slideUpView) {
[self createThumbScrollViewIfNecessary];
// create container view that will hold scroll view and label
CGRect frame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMaxY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(thumbScrollView.frame));
slideUpView = [[UIView alloc] initWithFrame:frame];
[slideUpView setBackgroundColor:[UIColor blackColor]];
[slideUpView setOpaque:NO];
[slideUpView setAlpha:0.75];
[[self view] addSubview:slideUpView];
// add subviews to container view
[slideUpView addSubview:thumbScrollView];
}
}
- (void)createThumbScrollViewIfNecessary {
if (!thumbScrollView) {
float scrollViewHeight = THUMB_HEIGHT + THUMB_V_PADDING;
float scrollViewWidth = CGRectGetWidth(self.view.bounds);
thumbScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth, scrollViewHeight)];
[thumbScrollView setCanCancelContentTouches:NO];
[thumbScrollView setClipsToBounds:NO];
// now place all the thumb views as subviews of the scroll view
// and in the course of doing so calculate the content width
float xPosition = THUMB_H_PADDING;
for (int i = 0; i < magazine.pageNum ; i++) {
Page *page = [magazine.pages objectAtIndex:i];
NSString *name = page.pageName;
NSString *mfjName =[name stringByReplacingOccurrencesOfString:#".mfj" withString:#"Small.mfj"];
UIImage *thumbImage = nil;
if([mfjName hasSuffix:#".mfj"])
thumbImage = [Reader loadMfjFromSprOrCache:magazine MFJ:mfjName];
ThumbImageView *thumbView;
if (thumbImage) {// sometimes mfjname is 0 which means white page in normal and black thumbnail in thumbnail scrollview.
if (!mThumbnailSizeUpdated) {
mThumbnailWidth = thumbImage.size.width;
mThumbnailHeight = thumbImage.size.height;
mThumbnailSizeUpdated = YES;
}
thumbView = [[ThumbImageView alloc] initWithImage:thumbImage];
} else {
CGRect thumbFrame;
if (mThumbnailSizeUpdated) {
thumbFrame = CGRectMake(0, 0, mThumbnailWidth, mThumbnailHeight);
} else {
mThumbnailWidth = 80;
mThumbnailHeight = 100;
thumbFrame = CGRectMake(0, 0, mThumbnailWidth, mThumbnailHeight);
}
thumbView = [[ThumbImageView alloc] initWithFrame:thumbFrame];
}
NSString *mfjPath= [[magazine getDownloadPath] stringByAppendingPathComponent:name];
if (![magazine getFileInfo:name]&&![[NSFileManager defaultManager] fileExistsAtPath:mfjPath]) {
thumbView.alpha = 0.5;
}
[thumbView setBackgroundColor:[UIColor blackColor]];
[thumbView setTag:THUMBVIEW_OFFSET+i];
[thumbView setDelegate:self];
[thumbView setImageName:name];
CGRect frame = [thumbView frame];
frame.origin.y = THUMB_V_PADDING;
frame.origin.x = xPosition;
frame.size.width = frame.size.width+30;
frame.size.height = frame.size.height+40;
[thumbView setFrame:frame];
[thumbScrollView addSubview:thumbView];
UILabel *pageIndexLabel = [[UILabel alloc] initWithFrame:CGRectMake(xPosition, frame.origin.y+frame.size.height-THUMB_LABEL_HEIGHT, frame.size.width, THUMB_LABEL_HEIGHT)];
[pageIndexLabel setBackgroundColor:[UIColor clearColor]];
[pageIndexLabel setText:[NSString stringWithFormat:#"%d",(i+1)]];
[pageIndexLabel setTextColor:[UIColor whiteColor]];
[pageIndexLabel setTextAlignment:UITextAlignmentCenter];
[thumbScrollView addSubview:pageIndexLabel];
xPosition += (frame.size.width + THUMB_H_PADDING);
}
thumbScrollView.showsHorizontalScrollIndicator = NO;
[thumbScrollView setContentSize:CGSizeMake(xPosition, scrollViewHeight)];
}
}
- (void)downloadPages
{
DownloadOperation *op = [[DownloadOperation alloc] initWithMagazine:magazine];
op.delegate = self;
[[(AppDelegate *)[[UIApplication sharedApplication] delegate] sharedOperationQueue] addOperation:op];
}
- (void)downloadOperation:(DownloadOperation *)operation finishedAtIndex:(NSUInteger)index
{
if (thumbScrollView){
[thumbScrollView viewWithTag:THUMBVIEW_OFFSET+index].alpha = 1.0;
}
}
In DownloadOperation.m
- (void)main
{
// ...
NSUInteger index = 0;
for (Page *page in mMagazine.pages)
{
if (/*page doesn't exist*/){
// download the page;
if ([delegate respondsToSelector:#selector(downloadOperation:finishedAtIndex:)]) {
[delegate downloadOperation:self finishedAtIndex:index];
}
}
index++;
}
}
you're using operation queue to download images -> thus, your finish callbacks might arrive not on the main thread, but you are trying to update you UI anyway - try wrapping your UI interaction into dispatch_async on main thread:
dispatch_async(dispatch_get_main_queue), ^{
if (thumbScrollView){
[thumbScrollView viewWithTag:THUMBVIEW_OFFSET+index].alpha = 1.0;
}
});
Thanks to #Inafziger for clues on this.