I have animation UIImageView that have 15 images.
UIImage *imageOne=[UIImage imageNamed:#"1.png"];
UIImage *imageTwo=[UIImage imageNamed:#"2.png"];
UIImage *imageThree=[UIImage imageNamed:#"3.png"];
UIImage *imageFour = [UIImage imageNamed:#"4.png"];
UIImage *imageFive = [UIImage imageNamed:#"5.png"];
UIImage *imageSix = [UIImage imageNamed:#"6.png"];
UIImage *imageSeven = [UIImage imageNamed:#"7.png"];
UIImage *imageEight = [UIImage imageNamed:#"8.png"];
UIImage *imageNine = [UIImage imageNamed:#"9.png"];
UIImage *imageTen = [UIImage imageNamed:#"10.png"];
UIImage *imageEleven = [UIImage imageNamed:#"11.png"];
UIImage *imageTwelve = [UIImage imageNamed:#"12.png"];
UIImage *imageThirteen = [UIImage imageNamed:#"13.png"];
UIImage *imageFourteen = [UIImage imageNamed:#"14.png"];
UIImage *imageFifty = [UIImage imageNamed:#"15.png"];
NSArray *arrayOfImages = [[NSArray alloc] initWithObjects:imageOne,imageTwo,imageThree,imageFour,imageFive,imageSix,imageSeven,imageEight,imageNine,imageTen,imageEleven,imageTwelve,imageThirteen,imageFourteen,imageFifty, nil];
self.imgView.animationDuration = 50.0;
self.imgView.animationImages= arrayOfImages;
[self.imgView startAnimating];
Now i want to get the name of current image when touch on UIImageView.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if([touch view] == self.imgView)
{
if([self.imgView.image isEqual:[UIImage imageNamed:#"1.png"]])
{
NSLog(#"1");
}
else if([self.imgView.image isEqual:[UIImage imageNamed:#"2.png"]])
{
NSLog(#"2");
}
}
}
Above code doesn't work. How can i do it?
You can't get the current image from an image view that's animating using animationImages. You'll need to loop through your array of images using a timer, and just set the image in the timer's action method.
Related
I need to access a random element from IBOutletCollection array. I have 4 letter imageViews that needs to be dragged on to another imageView and change the image depending on how many were dragged. Im testing with 2 at the moment. I have this but it doesn't seem to take random but a particular one of the 4.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UIImageView *letter in letterA)
{
for (UIImageView *letter1 in letterA)
{
if (CGRectIntersectsRect(letter.frame ,answerA.frame))
{
UIImage *Pic1 = [UIImage imageNamed:#"number1"];
[correctCounterA setImage:Pic1];
letter.userInteractionEnabled=NO;
letter.hidden=YES;
}
else if (CGRectIntersectsRect(letter1.frame, answerA.frame)) //CGRectIntersectsRect(letter.frame, answerA.frame))
{
UIImage *Pic2 = [UIImage imageNamed:#"number2"];
[correctCounterA setImage:Pic2];
}
}
}
}
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 implementing "scratch" functionality in my application. User scratches the screen and sees the image "below".
On touchesMoved: I update mask image and apply it to layer. In general code is like this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint cPoint = [touch locationInView:self];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
imageView.image = _maskImage;
// ... add some subviews to imageView corresponding to touch manner
_maskImage = [UIImage imageFromLayer:imageView.layer];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
_maskImageView.image = _maskImage;
_viewWithOurImage.layer.mask = _maskImageView.layer;
}
I get UIImage from CALayer using code (category on UIImage):
+ (UIImage*)imageFromLayer:(CALayer*)layer
{
UIGraphicsBeginImageContextWithOptions([layer frame].size, NO, 0);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
This code works perfectly on iOS6 (tested on iPhone 4s and iPad2), no lags at all.
But when I run it on iOS7 (xcode4 or xcode5), it is awfully slow and laggy. I used a time profiler, it clearly points to renderInContext: row.
Then I tried following code:
...
if (SYSTEM_VERSION_LESS_THAN(#"7.0"))
_maskImage = [UIImage imageFromLayer:imageView.layer];
else
_maskImage = [UIImage imageFromViewIniOS7:imageView];
...
+ (UIImage*)imageFromViewIniOS7:(UIView*)view
{
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0);
CGContextSetInterpolationQuality(UIGraphicsGetCurrentContext(), kCGInterpolationNone);
// actually there is NSInvocation, but I've shortened for example
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
And it is still very slow. Tested on iPhone 4s (same as was on iOS6), new iPod5 and iPad3.
What am I doing wrong? Obviously it's a problem with iOS7...
I will appreciate any suggestions.
I will suggest you to try some other way, as sorry to say, the touchesMoved function works slowly in IOS7, there's nothing wrong with your code