How to access a random element from IBOutletCollection array - ios

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];
}
}
}
}

Related

Why does NSArray of UIImageViews follow a certain pattern?

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.

How to get current ImageName from Animation UIImageView?

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.

i want to set tap gesture on every image that can be moved, but gesture is not set on every image

i want to add UIImageView on click of button add, and want to set tap gesture on every image that can be moved, somehow i added image but gesture is not set on every image
for add image
-(IBAction)addImageAction:(id)sender{
if (flag == true) {
imgNew = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 70, 70)];
flag = false;
}else{
imgNew = [[UIImageView alloc]initWithFrame:CGRectMake(imgNew.frame.origin.x+15, imgNew.frame.origin.y+15, 70, 70)];
}
[imgViewArray addObject:imgNew];
[viewMain addSubview:imgNew];
[imgNew setUserInteractionEnabled:YES];
[imgNew setImage:[UIImage imageNamed:#"Doraemon.png"]];
[self gestureMethod];
}
touch method
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == img1)
{
NSLog(#"its 1st image");
// set imageview to 1st image
img = img1;
}else if ([touch view] == img2){
NSLog(#"its second image");
// set imageview to 2nd image
img = img2;
}else {
img = imgNew;
}
[self gestureMethod];
NSLog(#"%#",touch.view.description);
}
in gestureMethod i done code for moving image and zoom in and zoom out

RenderInContext is incredibly slow on iOS7

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

how to drag dynamically created UIImageview in obj c (xcode)

i have gone through the many tutorials to drag an image on screen using touchesmoved method
but most of them are not for dynamically created imageviews
in my project i created 5 UIImageview programatically,
now i want that user can drag them on screen>> i also follow this answer
Question but all in vain , no success so far>
the code for creating the dynamically imageview is
for (int i = 0; i < index ; i++)
{
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:
[UIImageimageNamed:image_name]];
self.imageView1.userInteractionEnabled = YES;
imageView1.frame = CGRectMake(xvalue, yvalue, 80.0f, 80.0f);
self.view.userInteractionEnabled = YES;
[self.view addSubview:imageView1];
}
Thanks in advance for good replies
I'm a little confused with how you are creating UIImageViews because you are using a for-loop but a static name. So you might have 15 UIImageViews with the name "imageView1".
But anyways, this shouldn't matter at all if you are using the code in the question you linked to. Perhaps you put the lines in out of order... this is the proper order. (If it doesn't work let me know, along with the errors you get)
//-(void)ViewDidLoad? {???? is this your function??
int xvalue = 0;//?? not sure where you declared these
int yvalue = 0;//?? not sure where you declared these
for (int i = 0; i < index ; i++) {
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:
[UIImageimageNamed:image_name]];
self.imageView1.userInteractionEnabled = YES;
imageView1.frame = CGRectMake(xvalue, yvalue, 80.0f, 80.0f);
self.view.userInteractionEnabled = YES;
[self.view addSubview:imageView1];
}
self.elements=[myElements getElements];
imagesElements = [[NSMutableArray alloc]init];
for(ElemetsList *item in self.elements) {
UIImageView *oneelement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:item.imgElement]];
oneelement.frame = CGRectMake(item.positionX, item.positionY, item.width, item.height);
oneelement.userInteractionEnabled=YES;
[imagesElements addObject:oneelement];
}
for(UIImageView *img in imagesElements) {
[self.view addSubview:img];
}
//}? End Function, whatever it may be...?
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
for(UIImageView *img in imagesElements)
{
if([self view] == img)
{
CGPoint location = [touch locationInView:self.view];
img.center=location;
}
}
}
If you just want to hard-code in the moving for one image try this:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
imageView1.center=[touch locationInView:self.view];
}
There are plenty of ways to address this issue. You can add UIPanGestureRecognizer to each UIImageView at the time its creation, or you can use touchesBegan:, touchesMoved: on self (assuming it's a UIViewController) and traverse each UIImageView to see if its frame contains the point of the touch.

Resources