I am using Xcode 4.4 and target is iOS 5.
As on the title, I would like to create multiple pinch gestures for different images. I use two type of images. One is set as a background and the 2nd type of image is a stamp that is pasted on the image that is set as background. To do this I am using UIGestureRecognizer. Everything is fine until I paste the stamp type of image and try to zoom that image. I can zoom but when I zoom the image, the image has has been set as background zooms as well.
With the code below, I can paste different images (stamp type), tap and drag them individually but not when I try all the content zooms as well. Why isn't working?
Thank you in advance
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(#"menuListType=%# ; menuID=%#", self.category.menuListType ,self.menu.menuID);
UIImage *aImage;
NSString *imagepath = [NSString stringWithFormat:#"%#%#",[FileUtility getPDFImageFolderPath], self.menu.imageID];
if (self.menu.imageID == nil || [self.menu.imageID isEqualToString:#""]) {
// self.canvasView.image = [UIImage imageNamed:#"NotExistFile.jpg"];
aImage = [UIImage imageNamed:#"NotExistFile.jpg"];
} else {
// self.canvasView.image = [UIImage imageWithContentsOfFile:imagepath];
aImage = [UIImage imageWithContentsOfFile:imagepath];
// self.canvasView = [[[UIImageView alloc] initWithImage:self.canvasView.image] autorelease];
}
// Choose image base and set as background
if ([self.category.menuListType isEqualToString:#"4"]) {
//set size
int imageW = aImage.size.width;
int imageH = aImage.size.height;
float scale = (imageW > imageH ? 500.0f/imageH : 500.0f/imageW);
CGSize resizedSize = CGSizeMake(imageW * scale, imageH * scale);
UIGraphicsBeginImageContext(resizedSize);
[aImage drawInRect:CGRectMake(0, 0, resizedSize.width, resizedSize.height)];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.canvasView.contentMode = UIViewContentModeTop;
self.canvasView.image = resizedImage;
if (self.menu.imageID == nil || [self.menu.imageID isEqualToString:#""]) {
} else {
self.canvasView = [[[UIImageView alloc] initWithImage:self.canvasView.image] autorelease];
}
} else {
// Paste image and set as background
UIImage *captureImage;
NSString *capturepath = [NSString stringWithFormat:#"%#%#",[FileUtility getPDFImageFolderPath], #"capture.png"];
if ([FileUtility fileExist:capturepath]) {
captureImage = [UIImage imageWithContentsOfFile:capturepath];
} else {
captureImage = [UIImage imageNamed:#"NotExistFile3.png"];
}
self.canvasView.contentMode = UIViewContentModeTop;;
self.canvasView.image = captureImage;
self.canvasView = [[[UIImageView alloc] initWithImage:self.canvasView.image] autorelease];
// Prepare Gestures
iViewsTapidx = 0;
iViewsDblTapidx = 0;
ivMax = sizeof(iViews) / sizeof(iViews[0]);
isTaped = NO;
isDblTaped = NO;
_width = 100;
// Initialization code
// Single tap
UITapGestureRecognizer *tapg =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tapAction:)];
[self.itemView addGestureRecognizer:tapg];
// Double tap
UITapGestureRecognizer *doubleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(doubleTapAction:)];
doubleTap.numberOfTapsRequired = 2;
[self.itemView addGestureRecognizer:doubleTap];
// longPress(長押し)
UILongPressGestureRecognizer *longPress =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:#selector(longPressAction:)];
[self.itemView addGestureRecognizer:longPress];
// drag
UIPanGestureRecognizer *pan =
[[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(panAction:)];
[self.itemView addGestureRecognizer:pan];
//pinch
UIPinchGestureRecognizer *pinchGestureRognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinch:)];
[self.itemView addGestureRecognizer:pinchGestureRognizer];
// Stamp Image
int i = 0;
CGPoint tapPoint = CGPointMake(self.canvasView.center.x, self.canvasView.center.y);
int parcent = aImage.size.width / _width; // Prepare image to paste
// Show stamp
*(iViews+i) = [[UIImageView alloc] initWithImage:aImage];
(*(iViews+i)).frame = CGRectMake(tapPoint.x - aImage.size.width/parcent/2,
tapPoint.y - aImage.size.height/parcent/2,
aImage.size.width/parcent,
aImage.size.height/parcent);
(*(iViews+i)).tag = i+1;
[self.itemView addSubview:*(iViews+i)];
iViewsTapidx = i;
isTaped = YES;
self.itemView.userInteractionEnabled = YES;
}
[self showAnimation];
}
// tap
-(void)tapAction:(UITapGestureRecognizer *) sender{
CGPoint tapPoint = [sender locationInView:self.itemView];
NSLog(#">>>tap x=%.2f, y=%.2f", tapPoint.x, tapPoint.y);
int i =0;
isTaped = NO;
for (i = 0; i < ivMax; i++) {
if (CGRectContainsPoint((*(iViews+i)).frame, tapPoint)) {
isTaped = YES;
iViewsTapidx = i;
NSLog(#"i = %d", i);
break;
}
}
}
// doubleTap
-(void)doubleTapAction:(UITapGestureRecognizer *) sender{
NSLog(#">>>doubleTap");
CGPoint tapPoint = [sender locationInView:self.itemView];
isTaped = NO;
isDblTaped = NO;
int i =0;
for (i = 0; i < ivMax; i++) {
if (CGRectContainsPoint((*(iViews+i)).frame, tapPoint)) {
isDblTaped = YES;
iViewsDblTapidx = i;
break;
}
}
// remove
if (isDblTaped) {
NSLog(#"remove %d", i);
(*(iViews+i)).tag = 0;
[*(iViews+i) removeFromSuperview];
}
}
// longPress
- (void)longPressAction:(UILongPressGestureRecognizer *) sender{
if ([sender state] == UIGestureRecognizerStateBegan) {
NSLog(#">>>longPress 1");
}else if ([sender state] == UIGestureRecognizerStateEnded) {
CGPoint tapPoint = [sender locationInView:self.itemView];
NSLog(#">>>longPress 2 x=%.2f, y=%.2f", tapPoint.x, tapPoint.y);
int i =0;
for (i = 0; i < ivMax; i++) {
NSLog(#"i = %d", i);
if ((*(iViews+i)).tag == 0) {
break;
}
}
if (i < ivMax) {
//Stamp imapge
UIImage *stampImage;
NSString *imagepath = [NSString stringWithFormat:#"%#%#",[FileUtility getPDFImageFolderPath], self.menu.imageID];
if (self.menu.imageID == nil || [self.menu.imageID isEqualToString:#""]) {
stampImage = [UIImage imageNamed:#"NotExistFile.jpg"];
} else {
stampImage = [UIImage imageWithContentsOfFile:imagepath];
}
int parcent = stampImage.size.width / _width; // Set stamp image
//Sow stamp image
*(iViews+i) = [[UIImageView alloc] initWithImage:stampImage];
(*(iViews+i)).frame = CGRectMake(tapPoint.x - stampImage.size.width/parcent/2,
tapPoint.y - stampImage.size.height/parcent/2,
stampImage.size.width/parcent,
stampImage.size.height/parcent);
(*(iViews+i)).tag = i+1;
[self.itemView addSubview:*(iViews+i)];
iViewsTapidx = i;
isTaped = YES;
}
}
}
// drag
- (void)panAction:(UIPanGestureRecognizer *) sender{
NSLog(#">>>pan");
if (isTaped) {
CGPoint p = [sender translationInView:self.itemView];
CGPoint movePoint = CGPointMake((*(iViews+iViewsTapidx)).center.x + p.x,
(*(iViews+iViewsTapidx)).center.y + p.y);
(*(iViews+iViewsTapidx)).center = movePoint;
// NSLog(#">>>pan x=%.2f, y=%.2f --> x=%.2f, y=%.2f", p.x, p.y, movePoint.x, movePoint.y);
NSLog(#">>>pan x=%.2f, y=%.2f", p.x, p.y);
[sender setTranslation:CGPointZero inView:self.itemView];
}
}
-(void) handlePinch:(UIGestureRecognizer *) sender {
UIPinchGestureRecognizer *pinchGesture = (UIPinchGestureRecognizer *) sender;
if (pinchGesture.state == UIGestureRecognizerStateBegan || pinchGesture.state == UIGestureRecognizerStateChanged) {
//UIView *view = pinchGesture.view;
// UIView *view = self.itemView;
self.itemView.transform = CGAffineTransformScale(self.itemView.transform, pinchGesture.scale, pinchGesture.scale);
pinchGesture.scale = 1;
NSLog(#"zoom");
}
}
You will have to add Gesture Recognizer on Uiimageview, Look here for doing so : UIGestureRecognizer on UIImageView
Yet another way is to use UIScrolliew as your container instead of UIView and use the - (UIView*)viewForZoomingInScrollView:(UIScrollView *)aScrollView {
return imageView;
}
here imageView is the image you want to zoom,this will zoom that only image you are pinching its tested and works fine.
Related
Here is the image slider which works fine. When dragging manually the image changes and indicator also changes but when i add animation only image changes automatically not the indicator.
Instead of animating image i want to scroll image so that indicators change
just like it changes on dragging. Also i want to add timer to it.
#implementation DashboardViewController {
NSArray * animationArray;
}
#synthesize scroller = scroller;
#synthesize pageControl = pageControl;
-
(void) viewDidLoad {
[super viewDidLoad];
scroller.pagingEnabled = YES;
scroller.showsHorizontalScrollIndicator = NO;
scroller.delegate = self;
animationArray = [NSArray arrayWithObjects: [UIImage imageNamed: # "image1.jpg"],
[UIImage imageNamed: # "image2.jpg"],
[UIImage imageNamed: # "image3.jpg"],
[UIImage imageNamed: # "image4.png"],
[UIImage imageNamed: # "image5.jpg"],
nil
];
CGRect scrollFrame = CGRectMake(0, 0, self.view.frame.size.width, self.scroller.frame.size.height);
scroller.frame = scrollFrame;
self.scroller.contentSize = CGSizeMake(self.scroller.frame.size.width * animationArray.count, self.scroller.frame.size.height);
for (int i = 0; i < animationArray.count; i++) {
CGRect frame;
frame.origin.x = self.scroller.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scroller.frame.size;
UIImageView * imgView = [
[UIImageView alloc] initWithFrame: CGRectMake(self.scroller.frame.size.width * i, 0, self.scroller.frame.size.width, self.scroller.frame.size.height)
];
imgView.image = [animationArray objectAtIndex: i];
imgView.frame = frame;
imgView.animationImages = animationArray;
imgView.animationDuration = 8;
imgView.animationRepeatCount = 0;
[imgView startAnimating];
[self.scroller addSubview:imgView];
}
self.pageControl.currentPage = 0;
}
-
(void) scrollViewDidScroll: (UIScrollView * ) sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scroller.frame.size.width;
int page = floor((self.scroller.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
} -
(void) scrollViewWillBeginDragging: (UIScrollView * ) scrollView {
pageControlBeingUsed = NO;
}
-
(void) scrollViewDidEndDecelerating: (UIScrollView * ) scrollView {
[self setIndiactorForCurrentPage];
} -
(void) setIndiactorForCurrentPage {
uint page = scroller.contentOffset.x / scroller.frame.size.width;
[pageControl setCurrentPage: page];
}
-
(IBAction) changePage {
// Update the scroll view to the appropriate page
CGRect frame;
pageControl.currentPage = animationArray.count;
frame.origin.x = self.scroller.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scroller.frame.size;
[self.scroller scrollRectToVisible: frame animated: YES];
pageControlBeingUsed = YES;
}
-
(void) didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
self.scroller = nil;
self.pageControl = nil;
}
Instead of animating image i want to scroll image so that indicators change
just like it changes on dragging. Also i want to add timer to it.
I think you have to set your current page inside your for loop for automatic change pagecontrol with image.
//move this up
self.pageControl.currentPage = 0;
for (int i = 0; i < animationArray.count; i++) {
CGRect frame;
frame.origin.x = self.scroller.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scroller.frame.size;
UIImageView * imgView = [
[UIImageView alloc] initWithFrame: CGRectMake(self.scroller.frame.size.width * i, 0, self.scroller.frame.size.width, self.scroller.frame.size.height)
];
imgView.image = [animationArray objectAtIndex: i];
imgView.frame = frame;
imgView.animationImages = animationArray;
imgView.animationDuration = 8;
imgView.animationRepeatCount = 0;
[imgView startAnimating];
self.pageControl.currentPage = i; //set i to current page
[self.scroller addSubview:imgView];
}
How can i keep a UIVIew highlighted ( change alpha ) until another one is tapped? I have a uiscrollview in which i have loaded several uiview in order to achieve the thumbnail effect. I create them using this method :
_thumbnailScroll.delegate = self;
[_thumbnailScroll setBackgroundColor:[UIColor clearColor]];
[_thumbnailScroll setCanCancelContentTouches:NO];
_thumbnailScroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;
_thumbnailScroll.clipsToBounds = NO;
_thumbnailScroll.scrollEnabled = YES;
_thumbnailScroll.pagingEnabled = YES;
NSUInteger nimages = 0;
NSInteger tot=0;
CGFloat cx = 0;
for (nimages = 0; nimages < 5 ; nimages++) {
// NSString *imageName = [NSString stringWithFormat:#"thumb%lu.png", (nimages + 1)];
UIImage *image = [UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:#"newPics/thumb/thumb1.png" ]];
if (tot==5) {
break;
}
if (5==nimages) {
nimages=0;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = 150;
rect.size.width = 150;
rect.origin.x = cx;
rect.origin.y = 0;
imageView.frame = rect;
// NSString *propertyName = [NSString stringWithFormat:#"image%lu", (unsigned long)nimages];
//UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapGestureCapturedFor1:)];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onSingleTapGestureRecognized:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
singleTap.delegate = self;
// [imageView add]
[imageView addGestureRecognizer:singleTap];
//[singleTap1 release];
imageView.userInteractionEnabled = YES;
[_thumbnailScroll addSubview:imageView];
_thumbnailScroll.userInteractionEnabled = YES;
// [imageView release];
cx += imageView.frame.size.width+100;
tot++;
}
The above method initializes the image views inside the scroll view and adds a tap gesture to be captured in the following method :
- (void)onSingleTapGestureRecognized:(UITapGestureRecognizer *)singleTapGestureRecognizer
{
UIView *tappedView = [_thumbnailScroll hitTest:[singleTapGestureRecognizer locationInView: _thumbnailScroll] withEvent:nil];
// tappedView.alpha =1.0;
// NSLog(#"image view tag %ld" , (long)tappedView.tag);
// NSLog(#" Description %# " , tappedView.description );
// label.highlighted = YES;
tappedView.alpha = 0.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 5.0 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
tappedView.alpha = 1.0;;
});
}
Now this method highlights the tapped uiview for almost 5 seconds, i am trying to keep it highlighted until i tap on another uiview. How can i achieve that?
Remove this code:
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 5.0 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
tappedView.alpha = 1.0;;
});
And replace with this:
for(UIView *view in _thumbnailScroll.subviews) {
view.alpha = 1.0;
}
This prevents the alpha in your tapped view from going back to 1 after 5 seconds and ensures all the other views have an alpha of zero. Or for a more performant approach, set a pointer to the last view clicked, and instead of looping through each of your ScrollView's subviews, simply set the alpha of the view you've referenced to 1.0. So instead, you'd add a property called lastTappedView and then replace the above code with:
_lastTappedView.alpha = 1.0;
_lastTappedView = tappedView;
singleTap.numberOfTouchesRequired = 1;
numberOfTouch is read-only.
in an UIRotationGestureRecognizer i will do an action drawCircleView.
But this action should start max 10 times in a second.
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotateContainerView:)];
rotation.delegate = self;
[_containerView addGestureRecognizer:rotation];
- (void)rotateContainerView:(UIRotationGestureRecognizer*)sender
{
static CGFloat initialScale;
if (sender.state == UIGestureRecognizerStateBegan) {
initialScale = (_Ro + 0.1)*-1;
_Rotation=0;
}
_Ro = (sender.rotation*-1);
sleep(.1);
[self drawCircleView];
}
I have test the following
sleep(0.1);
[NSThread sleepForTimeInterval:.1];
NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow:0.1]];
[NSThread sleepForTimeInterval:.1];
But with all it seems that all the action goes in a queue.
How i can do this without a query queue?
Full code
//
// CLHoleEffect.m
//
// Created by Kevin Siml - Appzer.de on 2013/10/23.
// Copyright (c) 2013 Appzer.de. All rights reserved.
//
#import "CLSwirlEffect.h"
#import "UIView+Frame.h"
#interface CLSwirlCircle : UIView
#property (nonatomic, strong) UIColor *color;
#end
#interface CLSwirlEffect()
<UIGestureRecognizerDelegate>
#end
#implementation CLSwirlEffect
{
UIView *_containerView;
UIView *_container;
CLSwirlCircle *_circleView;
UILabel* circleLabel;
CGFloat _X;
CGFloat _Y;
CGFloat _R;
CGFloat _Ro;
CGFloat _Rotation;
}
#pragma mark-
+ (NSString*)defaultTitle
{
return NSLocalizedStringWithDefaultValue(#"CLSwirlEffect_DefaultTitle", nil, [CLImageEditorTheme bundle], #"Swirl", #"");
}
+ (BOOL)isAvailable
{
return ([UIDevice iosVersion] >= 5.0);
}
- (id)initWithSuperView:(UIView*)superview imageViewFrame:(CGRect)frame toolInfo:(CLImageToolInfo *)info
{
self = [super initWithSuperView:superview imageViewFrame:frame toolInfo:info];
if(self){
_containerView = [[UIView alloc] initWithFrame:frame];
[superview addSubview:_containerView];
_X = 0.5;
_Y = 0.5;
_R = 0.5;
_Ro = 0.5;
[self setUserInterface];
}
return self;
}
- (void)cleanup
{
[_containerView removeFromSuperview];
}
- (UIImage*)applyEffect:(UIImage*)image
{
CGFloat R = (_R + 0.1);
GPUImageSwirlFilter *stillImageFilter = [[GPUImageSwirlFilter alloc] init];
[stillImageFilter setAngle: _Ro];
[stillImageFilter setRadius:R];
[stillImageFilter setCenter:CGPointMake(_X,_Y)];
UIImage *quickFilteredImage = [stillImageFilter imageByFilteringImage:image];
return quickFilteredImage;
}
#pragma mark-
- (void)setUserInterface
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapContainerView:)];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panContainerView:)];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinchContainerView:)];
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotateContainerView:)];
pan.maximumNumberOfTouches = 1;
tap.delegate = self;
pan.delegate = self;
pinch.delegate = self;
rotation.delegate = self;
[_containerView addGestureRecognizer:tap];
[_containerView addGestureRecognizer:pan];
[_containerView addGestureRecognizer:pinch];
[_containerView addGestureRecognizer:rotation];
_circleView = [[CLSwirlCircle alloc] init];
_circleView.backgroundColor = [UIColor clearColor];
_circleView.color = [UIColor whiteColor];
[_containerView addSubview:_circleView];
[self drawCircleView];
}
#define DEGREES_TO_RADIANS(x) (M_PI * x / 180.0)
- (void)drawCircleView
{
CGFloat R = MIN(_containerView.width, _containerView.height) * (_R + 0.1) * 1.2;
_circleView.width = R;
_circleView.height = R;
_circleView.center = CGPointMake(_containerView.width * _X, _containerView.height * _Y);
[_circleView setNeedsDisplay];
[self.delegate effectParameterDidChange:self];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// if the gesture recognizers are on different views, don't allow simultaneous recognition
if (gestureRecognizer.view != otherGestureRecognizer.view)
return NO;
// if either of the gesture recognizers is the long press, don't allow simultaneous recognition
if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] || [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
return NO;
return YES;
}
- (void)tapContainerView:(UITapGestureRecognizer*)sender
{
CGPoint point = [sender locationInView:_containerView];
_X = MIN(1.0, MAX(0.0, point.x / _containerView.width));
_Y = MIN(1.0, MAX(0.0, point.y / _containerView.height));
[self drawCircleView];
if (sender.state == UIGestureRecognizerStateEnded){
[self.delegate effectParameterDidChange:self];
}
}
- (void)panContainerView:(UIPanGestureRecognizer*)sender
{
CGPoint point = [sender locationInView:_containerView];
_X = MIN(1.0, MAX(0.0, point.x / _containerView.width));
_Y = MIN(1.0, MAX(0.0, point.y / _containerView.height));
[self drawCircleView];
if (sender.state == UIGestureRecognizerStateEnded){
//[self.delegate effectParameterDidChange:self];
}
}
- (void)pinchContainerView:(UIPinchGestureRecognizer*)sender
{
static CGFloat initialScale;
if (sender.state == UIGestureRecognizerStateBegan) {
initialScale = (_R + 0.1);
}
_R = MIN(1.1, MAX(0.1, initialScale * sender.scale)) - 0.1;
[self drawCircleView];
if (sender.state == UIGestureRecognizerStateEnded){
// [self.delegate effectParameterDidChange:self];
}
}
- (void)rotateContainerView:(UIRotationGestureRecognizer*)sender
{
static CGFloat initialScale;
if (sender.state == UIGestureRecognizerStateBegan) {
initialScale = (_Ro + 0.1)*-1;
_Rotation=0;
}
_Ro = (sender.rotation*-1);
[self drawCircleView];
if (sender.state == UIGestureRecognizerStateEnded){
// [self.delegate effectParameterDidChange:self];
}
}
#end
#pragma mark- UI components
#implementation CLSwirlCircle
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
[self setNeedsDisplay];
}
- (void)setCenter:(CGPoint)center
{
[super setCenter:center];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rct = self.bounds;
rct.origin.x += 1;
rct.origin.y += 1;
rct.size.width -= 2;
rct.size.height -= 2;
CGContextSetStrokeColorWithColor(context, self.color.CGColor);
CGContextStrokeEllipseInRect(context, rct);
self.alpha = 1;
[UIView animateWithDuration:kCLEffectToolAnimationDuration
delay:1
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
self.alpha = 0;
}
completion:^(BOOL finished) {
}
];
}
#end
To restrict the method, rotateContainerView:, being called no more than ten times in a second you could timestamp the first call and compare each subsequent calls timestamp, allowing the method to complete only if the calls timestamp is greater than last calls timestamp plus 0.1 seconds.
Add a property of type NSDate -
#property (nonatomic, strong) NSDate *lastCall;
Then modify rotateContainerView: to something like this -
- (void)rotateContainerView:(UIRotationGestureRecognizer*)sender
{
static CGFloat initialScale;
NSDate *nowCall = [NSDate date];// timestamp
if (sender.state == UIGestureRecognizerStateBegan) {
lastCall = nowCall;
initialScale = (_Ro + 0.1)*-1;
_Rotation=0;
_Ro = (sender.rotation*-1);
[self drawCircleView];
}
else {
if ([nowCall timeIntervalSinceDate:lastCall] > 0.1) {
_Ro = (sender.rotation*-1);
[self drawCircleView];
lastCall = nowCall;
}
}
}
drawRect is what's actually drawing the view. Decouple your data changes and the drawing changes. Draw your circles from drawRect, but change the rotation from a data method that you call instead of your current calls to drawCircleView. If you need to update on intervals, use a NSTimer to schedule calls to a rotate method. But leave the drawing of the actual circles up to drawRect. That way, you have guaranteed rotations, but the drawing happens all the time (which I think is what you want).
Does that make sense?
My images do not appear in my UIImageView when i am in event, in my viewDidLoad no problem.
(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if ( event.subtype == UIEventSubtypeMotionShake )
{
int i;
int r;
for ( i = 1 ; i <= 5 ; )
{
r = (arc4random()%NbrCells) + 1;
if (GridCell[r].hidden == NO)
{
// View position.
CGFloat OriginX = GridCell[r].frame.origin.x;
CGFloat OriginY = GridCell[r].frame.origin.y;
CGFloat Height = GridCell[r].frame.size.height;
CGFloat Whidth = GridCell[r].frame.size.width;
[GridCell[r] removeFromSuperview];
GridCell[r] = nil;
GridCell[r] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
GridCell[r].frame = CGRectMake(OriginX, OriginY, Whidth, Height);
GridCell[r].hidden = NO;
[self.MyImage1 addSubview:GridCell[r]];
// [MyArray[r] addObject:[NSNumber numberWithInt:NO]];
i++;
sleep(1);
}
}
NSLog(#"Event stop, number delete = %d", i - 1);
}
if ( [super respondsToSelector:#selector(motionEnded:withEvent:)] )
[super motionEnded:motion withEvent:event];
return;
}
Many Thanks.
Alan10977.
i have added around 15 to 16 UIImageviews on my View using the following code
- (void) setUpCellsUsingImage: (UIImage *) masterImage
{
rows = 4;
cols = 4;
containerCellHeight=hight/4;
containerCellWidth=width/4;
NSInteger row, col;
CGImageRef tempSubImage;
CGRect tempRect;
CGFloat yPos, xPos;
UIImage * aUIImage;
UIImageView *label;
cellArray = [[NSMutableArray new] autorelease];
int i =0;
for (row=0; row < rows; row++) {
yPos = row * containerCellHeight;
for (col=0; col < cols; col++) {
xPos = col * containerCellWidth;
label = [[UIImageView alloc]init];
tempRect = CGRectMake(xPos, yPos, containerCellWidth, containerCellHeight);
tempSubImage = CGImageCreateWithImageInRect(masterImage.CGImage, tempRect);
aUIImage = [UIImage imageWithCGImage: tempSubImage];
imgView = [[UIImageView alloc] initWithImage:aUIImage];
imgView.tag =i;
i++;
NSLog(#"original tags = %d",label.tag);
[cellArray addObject: aUIImage];
aUIImage = nil;
CGImageRelease(tempSubImage);
}
}
}
now i know i can determine which imageview has been touched with the help of tag of imageView but i dont know how to check for the tags in touchesBegan method.. what can i possibly do to differentiate uiimageview on basis of touch??
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:imgView];
NSLog(#"touch %#",imgView.tag);
}
new code:
- (void) setUpCellsUsingImage: (UIImage *) masterImage
{
rows = 4;
cols = 4;
containerCellHeight=hight/4;
containerCellWidth=width/4;
NSInteger row, col;
CGImageRef tempSubImage;
CGRect tempRect;
CGFloat yPos, xPos;
UIImage * aUIImage;
UIImageView *label;
cellArray = [[NSMutableArray new] autorelease];
int i =0;
for (row=0; row < rows; row++) {
yPos = row * containerCellHeight;
for (col=0; col < cols; col++) {
xPos = col * containerCellWidth;
label = [[UIImageView alloc]init];
tempRect = CGRectMake(xPos, yPos, containerCellWidth, containerCellHeight);
tempSubImage = CGImageCreateWithImageInRect(masterImage.CGImage, tempRect);
aUIImage = [UIImage imageWithCGImage: tempSubImage];
imgView = [[UIImageView alloc] initWithImage:aUIImage];
[self.view addSubview:imgView]; // i add the uiimageview here
imgView =CGRectMake(xPos, yPos, containerCellWidth-1, containerCellHeight-1);
imgView.tag =i;
i++;
NSLog(#"original tags = %d",label.tag);
[cellArray addObject: aUIImage];
aUIImage = nil;
CGImageRelease(tempSubImage);
}
}
}
- (void)viewDidLoad {
UIImage *mImage = [UIImage imageNamed:#"menu.png"];
hight = mImage.size.height;
width = mImage.size.width;
[self setUpCellsUsingImage:mImage];
[`super viewDidLoad];`
}
Please try this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:imgView];
UIView *hitView = [self.view hitTest:location withEvent:event];
NSLog(#"hitView %#",hitView);
UIImageView *hitImageView = nil;
if([hitView isKindOfClass:[UIImageView class]]) {
hitImageView = (UIImageView *)hitImageView;
}
NSLog(#"touched %#", hitImageView);
}
You can use UIGestureRecognizers as of iOS 3.2. I would recommend using one UIImageView for the menu you have and adding the UITapGestureRecognizer to detect single taps. Then use locationInView in the action to see where in the image the user touched.
...
UITapGestureRecognizer *tapRecognizer =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(menuSelect:)];
[imageView addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
}
- (void)menuSelect:(UIGestureRecognizer *)gesture {
// get location CGPoint for touch from recognizer
}
This is a link to the very helpful Apple guide to recognizers.