this is a walkthrough open library on github that I want to use in my app.
https://github.com/GnosisHub/GHWalkThrough
there is a method to set up bg view:
- (UIImage*) bgImageforPage:(NSInteger)index
{
UIImage* image = [UIImage imageNamed:#"bgimage"];
return image;
}
And I wanted to add set different image for each index, so i did this:
- (UIImage*) bgImageforPage:(NSInteger)index {
UIImage* image;
if (index == 0) {
image = [UIImage imageNamed:#"screen 1"];
} else if (index == 1) {
image = [UIImage imageNamed:#"screen 2"];
} else if (index == 2) {
image = [UIImage imageNamed:#"screen 3"];
} else if (index == 3) {
image = [UIImage imageNamed:#"screen 4"];
}
return image;
}
Result:
whenever the view is loaded there is a clear bg, and if I swipe left to index 1 I get screen 1 > and if I swipe left for index 2, 3 & 4 the bg stays screen 1...
Can someone see what is wrong here?
i think you have minor mistak from code.
- (UIImage*) bgImageforPage:(NSInteger)index
{
NSString* imageName =[NSString stringWithFormat:#"bg_0%ld.jpg", index+1]; // you have manually add name but not increment index
UIImage* image = [UIImage imageNamed:imageName];
return image;
}
if you can't use bottom code & use upper code you got your result or same as use bottom code & use upper code then also you got same result.
- (UIImage*) bgImageforPage:(NSInteger)index
{
NSString* imageName =[NSString stringWithFormat:#"bg_0%ld.jpg", index+1];
UIImage* image;// = [UIImage imageNamed:imageName];
if (index == 0) {
image = [UIImage imageNamed:imageName];
} else if (index == 1) {
image = [UIImage imageNamed:imageName];
} else if (index == 2) {
image = [UIImage imageNamed:imageName];
} else if (index == 3) {
image = [UIImage imageNamed:imageName];
}
return image;
}
Please add image name in your app image folder like below.
(Note: your image name set below as in your project.)
Example Image Name :-
your condition wise you got new index but not getting images but when you set images name like same as upper images name & you got your images as index wise.
You don't seem to update index, which means it stays at 0.
If it stays at 0, the image will always be "screen 1".
Related
I have an array of 4 UIImageViews A, A2, A3 and A4. I want them to be dragged on to another imageView called answerA. Every time I drag one of them on the answer it should change the image on answerA to a number which tells how many were dragged on it. E.g. If i drag A on answerA then answerA image will change to number 1. If i drag one more A from the array then it will change to number 2. For some reason my array follows a strange pattern. The first one i drag changes to number 1, the second one does nothing, the third one will change to number 2 and the fourth will change it back to number 1. No matter in which order i drag them it will always be in same pattern.
This is my array of 4 imageViews
#property (strong, nonatomic) NSArray *letterA; //initialized in .h
self.letterA = #[A, A2, A3, A4];//initialized in .m
My method to check collision:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
int intersectionCount = 0;
for (UIImageView *letter in letterA) {
if (CGRectIntersectsRect(letter.frame ,answerA.frame)) {
letter.userInteractionEnabled = NO;
letter.hidden = YES;
intersectionCount++;
}
}
if (intersectionCount == 1) {
UIImage *Pic1 = [UIImage imageNamed:#"number1.png"];
[correctCounterA setImage:Pic1];
}
else if (intersectionCount == 2) {
UIImage *Pic2 = [UIImage imageNamed:#"number2.png"];
[correctCounterA setImage:Pic2];
}
else if (intersectionCount == 3) {
UIImage *Pic3 = [UIImage imageNamed:#"number3.png"];
[correctCounterA setImage:Pic3];
}
else if (intersectionCount == 4) {
UIImage *Pic4 = [UIImage imageNamed:#"number4.png"];
[correctCounterA setImage:Pic4];
}
NOTE: correctCounterA is a small imageView on top of answerA so that only that small section would change the image to numbers.
You need to modified touchesEnded as like below. here you are finding drag image in wrong way. may this help you.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UIImageView *letter in letterA) {
if (CGRectIntersectsRect(letter.frame ,answerA.frame)) {
letter.userInteractionEnabled = NO;
letter.hidden = YES;
intersectionCount++;
break;
}
}
if (intersectionCount == 1) {
UIImage *Pic1 = [UIImage imageNamed:#"number1.png"];
[correctCounterA setImage:Pic1];
}
else if (intersectionCount == 2) {
UIImage *Pic2 = [UIImage imageNamed:#"number2.png"];
[correctCounterA setImage:Pic2];
}
else if (intersectionCount == 3) {
UIImage *Pic3 = [UIImage imageNamed:#"number3.png"];
[correctCounterA setImage:Pic3];
}
else if (intersectionCount == 4) {
UIImage *Pic4 = [UIImage imageNamed:#"number4.png"];
[correctCounterA setImage:Pic4];
}
}
Edit : According to your requirement, you need to declare intersectionCount in .h file and in viewDidLoad, you need to assign as below.
int intersectionCount = 0;
and Change touchesEnded like as above.
I am creating a timer which is used to play a song for 6 seconds.But i want that whenever i clicked the button it will recognized the particular image which is animating at that particular instant.For that i used CALayer but it is not giving the image name.
This is my code:
-(void)playSong
{
timerButton.imageView.animationImages =[NSArray arrayWithObjects:[UIImage imageNamed:#"Timer6.png"],[UIImage imageNamed:#"Timer5.png"],
[UIImage imageNamed:#"Timer4.png"],[UIImage imageNamed:#"Timer3.png"],
[UIImage imageNamed:#"Timer2.png"],[UIImage imageNamed:#"Timer1.png"],
nil];
timerButton.imageView.animationDuration=6.0;
[self.player play];
[timerButton.imageView startAnimating];
}
- (void)handleLongPressGestures:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
{
CALayer *player1 = timerButton.imageView.layer;
}
}
Please help me.
Unfortunately, you cannot get the image name from a UIImageView or UIImage object.
You could, however, set an accessibility identifier to a UIImage object that, in your case, could be it's file name.
Then simply doing someUIImageObject.accessibilityIdentifier should return the file name.
(refer answer)
Example:
UIImage *image = [UIImage imageNamed:#"someImage.png"];
[image setAccessibilityIdentifier:#"someImage"];
NSLog(#"%#",image.accessibilityIdentifier);
Now... you'd expect timerButton.imageView.image.accessibilityIdentifier to give you the image name but that's not how it works when the UIImageView is animating.
This time, we need to access it via the UIImageViews animationImages array property.
For this, we will need some custom logic to get the UIImage object from this animationImages array.
We'll first record the time we started animating the images and with some basic maths, we can calculate the index of the animationImages array that is currently being displayed in the UIImageView.
(refer answer)
Answer (code):
-(void)playSong
{
NSMutableArray *arrImages = [[NSMutableArray alloc] init];
for(int i = 6 ; i > 0 ; i--) {
//Generate image file names: "Timer6.png", "Timer5.png", ..., "Timer1.png"
NSString *strImageName = [NSString stringWithFormat:#"Timer%d.png",i];
//create image object
UIImage *image = [UIImage imageNamed:strImageName];
//remember image object's filename
[image setAccessibilityIdentifier:strImageName];
[arrImages addObject:image];
}
[timerButton.imageView setAnimationImages:arrImages];
[timerButton.imageView setAnimationDuration:6.0f];
[timerButton.imageView setAnimationRepeatCount:0];
[self.player play];
[timerButton.imageView startAnimating];
//record start time
startDate = [NSDate date]; //declare "NSDate *startDate;" globally
}
- (void)handleLongPressGestures:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan) {
[self checkCurrentImage];
}
}
-(void)checkCurrentImage
{
//logic to determine which image is being displayed from the animationImages array
NSTimeInterval durationElapsed = -1 * [startDate timeIntervalSinceNow];
NSTimeInterval durationPerFrame = timerButton.imageView.animationDuration / timerButton.imageView.animationImages.count;
int currentIndex = (int)(durationElapsed / durationPerFrame) % timerButton.imageView.animationImages.count;
UIImage *imageCurrent = timerButton.imageView.animationImages[currentIndex];
NSString *strCurrentImage = imageCurrent.accessibilityIdentifier;
NSLog(#"%#",strCurrentImage);
}
I'm new to Xcode and programming and I am not sure how to set the image of the UIButton to change every time it is tapped. For example, if it was a clock the button would start at the 12 o clock position and if the button was tapped it would change the image to the 1 0 clock position and so on.
I have this method in my .h
-(IBAction)Hexa1Button:(id)sender;
and I am kinda stuck there.
Follow this sample logic .This may be useful for you.
-(void)viewDidLoad{
isCount = 0; // int value.
UIImage * image1 = [UIImage imageNamed:#"SampleImage1.png"];
UIImage * image2 = [UIImage imageNamed:#"SampleImage2.png"];
UIImage * image3 = [UIImage imageNamed:#"SampleImage3.png"];
UIImage * image4 = [UIImage imageNamed:#"SampleImage4..png"];
UIImage * image5 = [UIImage imageNamed:#"SampleImage5.png"];
UIImage * image6 = [UIImage imageNamed:#"SampleImage6.png"];
UIImage * image7 = [UIImage imageNamed:#"SampleImage7.png"];
UIImage * image8 = [UIImage imageNamed:#"SampleImage8.png"];
UIImage * image9 = [UIImage imageNamed:#"SampleImage9.jpg"];
imagesArray = [[NSMutableArray alloc]initWithObjects:image1,image2,image3,image4,image5,image6,image7,image8,image9, nil];
}
-(IBAction)Hexa1Button:(id)sender{
if( isCount < [imagesArray count]){
[Hexa1Button setBackgroundImage:[imagesArray objectAtIndex:isCount] forState:UIControlStateNormal];
isCount ++;
}
}
I am trying to check the current orientation of an image and flip it to the opposite of what it currently is.
This code works if I only have 1 element on my screen, but as soon as i increase the array its a 50/50 chance of getting the orientation right.
-(IBAction)flipBtn:(id)sender
{
UIImage* flippedImage;
if(flipBtn.tag==FLIP_BUTTON_TAG)
{
UIImage* sourceImage1 = self.outletImageView.image;
flippedImage = [UIImage imageWithCGImage:sourceImage1.CGImage scale:1.0 orientation: UIImageOrientationUpMirrored];
flipBtn.tag = FLIP_VUTTON_TAG2;
}else{
UIImage* sourceImage1 = self.outletImageView.image;
flippedImage = [UIImage imageWithCGImage:sourceImage1.CGImage scale:1.0 orientation: UIImageOrientationUp];
flipBtn.tag = FLIP_BUTTON_TAG;
}
NSInteger index1 = [imgViewArray indexOfObject:self.outletImageView];
[imgViewArray removeObject:self.outletImageView];
self.outletImageView.image = flippedImage;
[imgViewArray insertObject:self.outletImageView atIndex:index1];
}
Instead of: [imgViewArray removeObject:self.outletImageView];
Write: [imgViewArray removeObjectAtIndex:index1];
And afterwards you need to check flippedImage.imageOrientation == UIImageOrientationUp to see if it's inverted or not and make the appropriate change.
You can verify which orientation the picture has by accessing the info dictionary [info objectForKey:#"Orientation"]
Here is a method that will return the new desired orientation based on your previous check (you'd pass flippedImage.imageOrientation as the argument):
- (UIImageOrientation)orientation:(int)orientation
{
UIImageOrientation newOrientation;
switch (orientation)
{
case 1:
newOrientation = UIImageOrientationUp;
break;
case 2:
newOrientation = UIImageOrientationUpMirrored;
break;
}
return newOrientation;
}
I have a view controller,on top of this I have added a image view.Now I wanted to load some image dynamically to that image view.Fallowing is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
if (getAnySelectedRow == 0)
{
//Load corresponding image
NSLog(#"1 image loaded");
}
else if (getAnySelectedRow == 1)
{
//Load corresponding image
NSLog(#"2 image loaded");
}
else if (getAnySelectedRow == 2)
{
//Load corresponding image
NSLog(#"3 image loaded");
}
}
Note: Image view is already loaded on top of view controller.So I believe no need to init new image view.
How can I do this?
This is actually quite simple. UIImageView, has an image property of type UIImage. All you have to do is create a UIImage object and then assign it to this property. If the image exists within your applications bundle, you can use +[UIImage imageNamed:]`.
myImageView.image = [UIImage imageNamed:#"myImageName"];
Or to expand a little further..
if (getAnySelectedRow == 0) {
myImageView.image = [UIImage imageNamed:#"myImageName"];
}else if (getAnySelectedRow == 1) {
myImageView.image = [UIImage imageNamed:#"myOtherImageName"];
}else{
myImageView.image = [UIImage imageNamed:#"myAnotherDifferentImageName"];
}
x, y are the position the image view located,
width and height are the size of the ImageView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
imageView.image = [UIImage imageNamed:#"your image"];