ios - How to prevent button resize when zoom super view? -
i adding multiple gestures in 1 view, view have 1 close button @ corner of view, working fine when zoom view close button zoom view, want zoom view not close button, please suggest me how ?
see below image reference.
code pinch zoom
-(void)addstickerswithview:(uiview*)view image:(uiimage*)image{ cgpoint center = self.imgphoto.center; uiimageview *imgview = [[uiimageview alloc] initwithimage:image]; uirotationgesturerecognizer *rotationgesture = [[uirotationgesturerecognizer alloc] initwithtarget:self action:@selector(rotatepiece:)]; [imgview setcontentmode:uiviewcontentmodescaletofill]; uiview *viewzoom = [[uiview alloc] initwithframe:cgrectmake(center.x-45,center.y-45, 90, 90)]; // [viewzoom setbackgroundcolor:[uicolor redcolor]]; imgview.frame = cgrectmake(5, 5, cgrectgetwidth(viewzoom.frame)-10, cgrectgetheight(viewzoom.frame)-10); [viewzoom addsubview:imgview]; [viewzoom addgesturerecognizer:rotationgesture]; uipinchgesturerecognizer *pinchgesture = [[uipinchgesturerecognizer alloc] initwithtarget:self action:@selector(scalepiece:)]; [pinchgesture setdelegate:self]; [viewzoom addgesturerecognizer:pinchgesture]; uipangesturerecognizer *pangesture = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(moveimage:)]; [pangesture setminimumnumberoftouches:1]; [pangesture setmaximumnumberoftouches:1]; [viewzoom addgesturerecognizer:pangesture]; uibutton *btnclosesticker = [uibutton buttonwithtype:uibuttontypecustom]; [btnclosesticker setbounds:cgrectmake(0, 0, 30,30)]; [btnclosesticker setimage:[uiimage imagenamed:@"close1.png"] forstate:uicontrolstatenormal]; [btnclosesticker addtarget:self action:@selector(buttonpressed:) forcontrolevents:uicontroleventtouchupinside]; self.stickercount++; btnclosesticker.tag = self.stickercount; uitapgesturerecognizer *tapgesture = [[uitapgesturerecognizer alloc]initwithtarget:self action:@selector(taptoshowclose:)]; tapgesture.numberoftapsrequired = 2; [viewzoom addgesturerecognizer:tapgesture]; viewzoom.layer.bordercolor = [uicolor whitecolor].cgcolor; viewzoom.layer.borderwidth = 3.0; viewzoom.tag = self.stickercount+ktagborder; [viewzoom addsubview:btnclosesticker]; [view addsubview:viewzoom]; } -(void)hideshowborderclosebutton{ int bordercount = self.stickercount-ktagborder; for(int i=1;i<=bordercount;i++){ uiview *view = [self.viewstickers viewwithtag:i+ktagborder]; uiview *view1 = [self.viewstickers viewwithtag:i+(ktagborder*2)]; view.hidden = yes; view1.layer.borderwidth = 0.0; } } -(void)buttonpressed:(id)sender{ uiview *view = [sender superview]; [view removefromsuperview]; self.stickercount--; } - (void)scalepiece:(uipinchgesturerecognizer *)gesturerecognizer { if([gesturerecognizer state] == uigesturerecognizerstatebegan) { // reset last scale, necessary if there multiple objects different scales lastscale = [gesturerecognizer scale]; } uibutton *btn = [self.viewstickers viewwithtag:gesturerecognizer.view.tag-ktagborder]; if ([gesturerecognizer state] == uigesturerecognizerstatebegan || [gesturerecognizer state] == uigesturerecognizerstatechanged) { cgfloat currentscale = [[[gesturerecognizer view].layer valueforkeypath:@"transform.scale"] floatvalue]; // constants adjust max/min values of zoom const cgfloat kmaxscale = 3.0; const cgfloat kminscale = 1.0; cgfloat newscale = 1 - (lastscale - [gesturerecognizer scale]); newscale = min(newscale, kmaxscale / currentscale); newscale = max(newscale, kminscale / currentscale); cgaffinetransform transform = cgaffinetransformscale([[gesturerecognizer view] transform], newscale, newscale); [gesturerecognizer view].transform = transform; lastscale = [gesturerecognizer scale]; // store previous scale factor next pinch gesture call } } -(void)taptoshowclose:(uitapgesturerecognizer *)gesturerecognizer{ //int bordercount = self.stickercount-ktagborder; uiview *view = gesturerecognizer.view; //cast pointer derived class if needed view.layer.borderwidth = 2.0; uiview *view1 = [self.viewstickers viewwithtag:view.tag-ktagborder]; [(uibutton*)view1 setimage:[uiimage imagenamed:@"close1.png"] forstate:uicontrolstatenormal]; [view1 setbounds:cgrectmake(0, 0, 30,30)]; view1.hidden = no; } - (void)moveimage:(uipangesturerecognizer *)recognizer { cgpoint newcenter = [recognizer translationinview:self.view]; [self hideshowborderclosebutton]; if([recognizer state] == uigesturerecognizerstatebegan) { beginx = recognizer.view.center.x; beginy = recognizer.view.center.y; } newcenter = cgpointmake(beginx + newcenter.x, beginy + newcenter.y); [recognizer.view setcenter:newcenter]; } - (bool)gesturerecognizer:(uigesturerecognizer *)gesturerecognizer shouldrecognizesimultaneouslywithgesturerecognizer:(uigesturerecognizer *)othergesturerecognizer { // if gesture recognizers on different views, don't allow simultaneous recognition if (gesturerecognizer.view != othergesturerecognizer.view) return no; // if either of gesture recognizers long press, don't allow simultaneous recognition if ([gesturerecognizer iskindofclass:[uilongpressgesturerecognizer class]] || [othergesturerecognizer iskindofclass:[uilongpressgesturerecognizer class]]) return no; return yes; } - (void)rotatepiece:(uirotationgesturerecognizer *)gesturerecognizer { [self adjustanchorpointforgesturerecognizer:gesturerecognizer]; [self hideshowborderclosebutton]; if ([gesturerecognizer state] == uigesturerecognizerstatebegan || [gesturerecognizer state] == uigesturerecognizerstatechanged) { [gesturerecognizer view].transform = cgaffinetransformrotate([[gesturerecognizer view] transform], [gesturerecognizer rotation]); [gesturerecognizer setrotation:0]; } } - (void)adjustanchorpointforgesturerecognizer:(uigesturerecognizer *)gesturerecognizer { [self hideshowborderclosebutton]; if (gesturerecognizer.state == uigesturerecognizerstatebegan) { uiview *piece = gesturerecognizer.view; cgpoint locationinview = [gesturerecognizer locationinview:piece]; cgpoint locationinsuperview = [gesturerecognizer locationinview:piece.superview]; piece.layer.anchorpoint = cgpointmake(locationinview.x / piece.bounds.size.width, locationinview.y / piece.bounds.size.height); piece.center = locationinsuperview; } }
in place of scaling view. it's batter calculate frame zoom level. , update frame of view. check out below method.
- (ibaction)scalepiece:(uipinchgesturerecognizer *)gesturerecognizer { if([gesturerecognizer state] == uigesturerecognizerstatebegan) { // reset last scale, necessary if there multiple objects different scales lastscale = [gesturerecognizer scale]; } if ([gesturerecognizer state] == uigesturerecognizerstatebegan || [gesturerecognizer state] == uigesturerecognizerstatechanged) { cgfloat newscale = 1 - (lastscale - [gesturerecognizer scale]); [self changescale:newscale]; lastscale = [gesturerecognizer scale]; // store previous scale factor next pinch gesture call } } -(void)changescale :(float)newscale{ cgaffinetransform transform = cgaffinetransformscale([viewforpinch transform], newscale, newscale); viewforpinch.transform = transform; float scale = viewforpinch.transform.a; float buttonscale = 1 / scale; btnl.transform= cgaffinetransformscale(cgaffinetransformidentity, buttonscale, buttonscale); }
Comments
Post a Comment