ios ktphotobrowser set contentsize of scrollview to zommed image size - iphone

i am using ktphotobrowser for my app. in big image screen (ktphotoscrollviewcontroller) when i zoom image with two times tapping, the content size of scrollview (ktphotoview - it is an uiscrollview object) grows to bound.size * maximumzoomscale but my zoomed image size still smaller than contentsize of scrollview. both zoomed image size and the scrollview's contentsize must be same so where can i do these calculation on the code?
if anybody use ktphotobrowser could help me about this problem, i will very appreciate.
i find the tap count but after this code i couldnt find where is the contentsize growing (zoom) or image growing (zoom) code
below you can find the whole code of ktphotoview.h page where the tapping count and zooming actions are coded:
#import "KTPhotoView.h"
#import "KTPhotoScrollViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface KTPhotoView (KTPrivateMethods)
- (void)loadSubviewsWithFrame:(CGRect)frame;
- (BOOL)isZoomed;
- (void)toggleChromeDisplay;
#end
#implementation KTPhotoView
#synthesize scroller = scroller_;
#synthesize index = index_;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setDelegate:self];
[self setMaximumZoomScale:2.0];
[self setShowsHorizontalScrollIndicator:NO];
[self setShowsVerticalScrollIndicator:NO];
[self loadSubviewsWithFrame:frame];
NSLog(#"scrollview2 %f",self.contentSize.height);
}
return self;
}
- (void)loadSubviewsWithFrame:(CGRect)frame
{
imageView_ = [[UIImageView alloc] initWithFrame:frame];
[imageView_ setContentMode:UIViewContentModeScaleAspectFit];
[self addSubview:imageView_];
}
- (void)setImage:(UIImage *)newImage
{
[imageView_ setImage:newImage];
}
- (void)layoutSubviews
{
[super layoutSubviews];
if ([self isZoomed] == NO && CGRectEqualToRect([self bounds], [imageView_ frame]) == NO) {
[imageView_ setFrame:[self bounds]];
}
}
- (void)toggleChromeDisplay
{
if (scroller_) {
[scroller_ toggleChromeDisplay];
}
}
- (BOOL)isZoomed
{
return !([self zoomScale] == [self minimumZoomScale]);
}
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
{
CGRect zoomRect;
// the zoom rect is in the content view's coordinates.
// At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
// As the zoom scale decreases, so more content is visible, the size of the rect grows.
zoomRect.size.height = [self frame].size.height / scale;
zoomRect.size.width = [self frame].size.width / scale;
// choose an origin so as to get the right center.
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
NSLog(#"zoomRectHeight %f",[self frame].size.height);
return zoomRect;
}
- (void)zoomToLocation:(CGPoint)location
{
float newScale;
CGRect zoomRect;
if ([self isZoomed]) {
zoomRect = [self bounds];
} else {
newScale = [self maximumZoomScale];
zoomRect = [self zoomRectForScale:newScale withCenter:location];
}
// NSLog(#"zoomRectHeight %f",location.);
[self zoomToRect:zoomRect animated:YES];
}
- (void)turnOffZoom
{
if ([self isZoomed]) {
[self zoomToLocation:CGPointZero];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch view] == self) {
if ([touch tapCount] == 2) {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(toggleChromeDisplay) object:nil];
[self zoomToLocation:[touch locationInView:self]];
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch view] == self) {
if ([touch tapCount] == 1) {
[self performSelector:#selector(toggleChromeDisplay) withObject:nil afterDelay:0.5];
}
}
}
#pragma mark -
#pragma mark UIScrollViewDelegate Methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
UIView *viewToZoom = imageView_;
NSLog(#"scrollview %f",self.contentSize.height);
return viewToZoom;
}

i use the below code in ktphotoview.m file to solve my problem hope this helps:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch view] == self) {
if ([touch tapCount] == 2) {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(toggleChromeDisplay) object:nil];
[self zoomToRect:[self zoomRectForScale:[self maximumZoomScale] withCenter:[touch locationInView:self]] animated:YES];
float abc=(self.contentSize.height-(imageView_.image.size.height*320/imageView_.image.size.width*self.maximumZoomScale))/2 *-1;
[self setContentInset:UIEdgeInsetsMake(abc, 0, abc, 0)];
}
}
}

Related

How to pass the touches to view from scrollview

I have 3 tableviews placed upon scrollview at x = 0.0, x = 320.0 and x = 640.0.
When the user horizontally swipes the tableviews (as they are on top), I want to pass the swipe event to its superview and when the user vertically swipes tableviews the tableview must scroll vertically.
How can I achieve this?
To pass the touch from UIScrollView, use this code as a category:
#implementation UIScrollView (FixedApi)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
NSLog(#"touch view = %#", [[touch view].class description]);
if ([[[touch view].class description] isEqualToString:#"UITableViewCellContentView"]) {
//To pass the touch to UITableViewCell
} else if ([[[touch view].class description] isEqualToString:#"UITableView"] && isHorizntalSCroll == true && pageNumber == 2) {
//To pass the touch to UITableView
} else if ([[[touch view].class description] isEqualToString:#"UIView"]) {
//To pass the touch to UIView
} else {
[self.superview touchesBegan:touches withEvent:event]; // or 1 nextResponder, depends
[super touchesBegan:touches withEvent:event];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ( !self.dragging ) [self.nextResponder.nextResponder touchesEnded:touches withEvent:event];
[super touchesEnded:touches withEvent:event];
}
#end
To determine the vertical/Horizontal scrolling, you can use this code:
.h file <UIScrollViewDelegate>
BOOL pageControlIsChangingPage;
CGPoint startPos;
int scrollDirection;
int CX; //The width of the pages.
BOOL isHorizntalSCroll;
int pageNumber;
.m file
#pragma mark -
#pragma mark UIScrollViewDelegate stuff
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView {
if (scrollDirection==0){//we need to determine direction
//use the difference between positions to determine the direction.
if (abs(startPos.x-scrollView.contentOffset.x)<abs(startPos.y-scrollView.contentOffset.y)){
NSLog(#"Vertical Scrolling");
scrollDirection=1;
isHorizntalSCroll = false;
[scrollView setPagingEnabled:NO];
} else {
NSLog(#"Horitonzal Scrolling");
scrollDirection=2;
isHorizntalSCroll = ture;
[scrollView setPagingEnabled:YES];
}
}
if (scrollDirection==1) {
[scrollView setContentOffset:CGPointMake(startPos.x,scrollView.contentOffset.y) animated:NO];
} else if (scrollDirection==2){
[scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x,startPos.y) animated:NO];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView {
if (pageControlIsChangingPage) {
return;
}
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
pageNumber = page;
NSLog(#"page number = %d",page);
if (page == 3 || page == 0) {
[scrollView setContentSize:CGSizeMake(CX, personalInfo.frame.size.height + 100)];
} else {
[scrollView setContentSize:CGSizeMake(CX, [scrollView bounds].size.height)];
}
pageControlIsChangingPage = NO;
}
#pragma mark -
#pragma mark PageControl stuff
- (IBAction)changePage:(id)sender {
/*
* Change the scroll view
*/
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * pageControl.currentPage;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
/*
* When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off
*/
pageControlIsChangingPage = YES;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollViews{
startPos = scrollView.contentOffset;
scrollDirection=0;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollViews willDecelerate:(BOOL)decelerate {
if (decelerate) {
scrollDirection=3;
}
}
That's it,
disable horizontal scroling for each tableview via IB or code and set the contentsize of the base scroll view to be 960,heightOfTableview and you should be just fine... this should get you going.

Limitting UIImageView size while using CGAffineTransform

I want to limit my uiimageview size while using CGAffineTransform. I am limitting scale but I can't limit the size of the my uiimageview. When I run the app I see it is limitted but in the background the size of my uiimageview keeps increasing. How can I make this?
Here is my code:
- (id)initWithFrame:(CGRect)frame
{
if ([super initWithFrame:frame] == nil) {
return nil;
}
originalTransform = CGAffineTransformIdentity;
touchBeginPoints = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = YES;
self.exclusiveTouch = YES;
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSMutableSet *currentTouches = [[[event touchesForView:self] mutableCopy] autorelease];
[currentTouches minusSet:touches];
if ([currentTouches count] > 0) {
[self updateOriginalTransformForTouches:currentTouches];
[self cacheBeginPointForTouches:currentTouches];
}
[super touchesBegan:touches withEvent:event];
[self cacheBeginPointForTouches:touches];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGAffineTransform incrementalTransform = [self incrementalTransformWithTouches:[event touchesForView:self]];
self.transform = CGAffineTransformConcat(originalTransform, incrementalTransform);
CGAffineTransform transform = self.transform;
float scale = sqrt(transform.a*transform.a + transform.c*transform.c);
NSLog(#"%f",self.frame.size.height);
if (scale > SCALE_MAX){
self.transform = CGAffineTransformScale(transform, SCALE_MAX/scale, SCALE_MAX/scale);
}
else if (scale < SCALE_MIN){
self.transform = CGAffineTransformScale(transform, SCALE_MIN/scale, SCALE_MIN/scale);
}
[super touchesMoved:touches withEvent:event];
}
-(void)updateOriginalTransformForTouches:(NSSet *)touches{
CGAffineTransform transform = self.transform;
float scale = sqrt(transform.a*transform.a + transform.c*transform.c);
if (scale > SCALE_MAX){
self.transform = CGAffineTransformScale(transform, SCALE_MAX/scale, SCALE_MAX/scale);
}
else if (scale < SCALE_MIN){
self.transform = CGAffineTransformScale(transform, SCALE_MIN/scale, SCALE_MIN/scale);
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
if (touch.tapCount >= 2) {
[self.superview bringSubviewToFront:self];
}
}
[self updateOriginalTransformForTouches:[event touchesForView:self]];
[self removeTouchesFromCache:touches];
NSMutableSet *remainingTouches = [[[event touchesForView:self] mutableCopy] autorelease];
[remainingTouches minusSet:touches];
[super touchesEnded:touches withEvent:event];
[self cacheBeginPointForTouches:remainingTouches];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}
You can know CGRect of UIImagView before applying transform on it like below example :
//Create transform
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1.2, 1.2);
//Apply transform on UIImageView to get CGRect
CRect newRect = CGRectApplyAffineTransform(yourImgView.frame, scaleTransform);
//Check if rect is in valid limitation
if(newRect.size.height > 200 && newRect.size.width >200) //your condition to limit UIImageView's Size
{
//Valid so apply transform
yourImageView.transform = scaleTransform
}
else
{
//rect increases so no transform
// no transform to UIImageView
NSLog(#"%#",NSStringFromCGRect(yourImageView.frame))
}
I was facing the a similar problem, I have an image view that's rotating according to compass heading data and also has to move position according to device change in interface orientation
I created a tempImageView to help maintain frame, and kept reseting it with every change in device orientation
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
// equal tempImageView to imageView
_tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-75, 150, 64, 61)];
[_tempImageView setImage:[UIImage imageNamed:#"image.gif"]];
_tempImageView.transform = _imageView.transform;
// reset imageView
[_imageView removeFromSuperview];
_imageView = _tempImageView;
[self.view insertSubview:_imageView atIndex:1];
}

UIImage can not detect touch

I used codes below to detect the touch on UIImage
//
#import <UIKit/UIKit.h>
#interface KUIImageView : UIImageView
{
CGPoint startLocation;
}
#end
#implementation KUIImageView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
// Retrieve the touch point
CGPoint pt = [[touches anyObject] locationInView:self];
startLocation = pt;
[[self superview] bringSubviewToFront:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
// Move relative to the original touch point
CGPoint pt = [[touches anyObject] locationInView:self];
CGRect frame = [self frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[self setFrame:frame];
}
#end
I set the breakpoint at touchesBegan
But it was not triggered
the UIImage was on a UIScrollView by using
[aScrollView addObject:aUIImageView];
Welcome any comment
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// here
self.userInteractionEnabled = YES;
}
return self;
}

drawing on the iPhone in objective c [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Drawing on the iPhone in objective c
I have written this...
In the .h file:
#import <UIKit/UIKit.h>
#interface DrawView : UIView {
CGPoint gestureStartPoint,currentPosition;
CGContextRef c;
UIBezierPath *currentPath;
}
#property(nonatomic,retain)UIBezierPath *currentPath;
#end
And in the .m file:
#import "DrawView.h"
#implementation DrawView
#synthesize currentPath;
- (id)initWithFrame:(CGRect)frame {
[self drawRect:CGRectMake(0, 0, 320, 480)];
self = [super initWithFrame:frame];
if (self) {
currentPath = [[UIBezierPath alloc]init];
currentPath.lineWidth=3;
}
return self;
}
- (void)drawRect:(CGRect)rect {
[[UIColor redColor] set];
[currentPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentPosition = [touch locationInView:self];
[currentPath addLineToPoint:(currentPosition)];
[self setNeedsDisplay];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:self];
[currentPath moveToPoint:(gestureStartPoint)];
}
- (void)dealloc {
[super dealloc];
}
#end
I want to let the user draw an image on the iPhone screen and then use that image for the game... but this doesn't draw anything...
This is because your currentPath is not allocated.
If you instantiate the view in the resource [UIView initWithFrame:] will never be called.
Implement [UIView initWithCoder:] and allocate it there.
- (void)commonInit
{
currentPath = [[UIBezierPath alloc] init];
currentPath.lineWidth=3;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder*)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self commonInit];
}
return self;
}
Alternatively you can make the path in the touchesBegan if it is nil.
- (UIBezierPath*)currentPath
{
if (currentPath = nil)
{
currentPath = [[UIBezierPath alloc] init];
currentPath.lineWidth=3;
}
return currentPath;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:self];
[[self currentPath] moveToPoint:(gestureStartPoint)];
}
However, there are some problems in the code.
You call [self drawRect:CGRectMake(0, 0, 320, 480)] before self = [super initWithFrame:frame]. You must not call any method there. You can write your code only inside
if (self ! =nil)
{
// your code
}
currentPath leaks. Release it in dealloc.
Never call drawRect directly. Call [UIView setNeedsDisplay] instead.

Multi touch is not working perfectly on UIImageView

I am doing multi touch on UImageView means zoom in and zoom out on image view. I am using followng code but it doesn't work very well. Can anyone look at this code,
#import "ZoomingImageView.h"
#implementation ZoomingImageView
#synthesize zoomed;
#synthesize moved;
define HORIZ_SWIPE_DRAG_MIN 24
define VERT_SWIPE_DRAG_MAX 24
define TAP_MIN_DRAG 10
CGPoint startTouchPosition;
CGFloat initialDistance;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
moved = NO;
zoomed = NO;
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
}
- (void)dealloc {
if([timer isValid])
[timer invalidate];
[super dealloc];
}
- (void) setImage: (UIImage*)img
{
zoomed = NO;
moved = NO;
self.transform = CGAffineTransformIdentity;
[super setImage:img];
}
- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint {
float x = toPoint.x - fromPoint.x;
float y = toPoint.y - fromPoint.y;
return sqrt(x * x + y * y);
}
- (CGFloat)scaleAmount: (CGFloat)delta {
CGFloat pix = sqrt(self.frame.size.width * self.frame.size.height);
CGFloat scale = 1.0 + (delta / pix);
return scale;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if([timer isValid])
[timer invalidate];
moved = NO;
switch ([touches count]) {
case 1:
{
// single touch
UITouch * touch = [touches anyObject];
startTouchPosition = [touch locationInView:self];
initialDistance = -1;
break;
}
default:
{
// multi touch
UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
initialDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self]
toPoint:[touch2 locationInView:self]];
break;
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
if([timer isValid])
[timer invalidate];
/*if ([touches count] == 1) {
CGPoint pos = [touch1 locationInView:self];
self.transform = CGAffineTransformTranslate(self.transform, pos.x - startTouchPosition.x, pos.y - startTouchPosition.y);
moved = YES;
return;
}****/
if ((initialDistance > 0) && ([touches count] > 1)) {
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
CGFloat currentDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self]
toPoint:[touch2 locationInView:self]];
CGFloat movement = currentDistance - initialDistance;
NSLog(#"Touch moved: %f", movement);
CGFloat scale = [self scaleAmount: movement];
self.transform = CGAffineTransformScale(self.transform, scale, scale);
// }
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
if ([touches count] == 1) {
// double tap to reset to default size
if ([touch1 tapCount] > 1) {
if (zoomed) {
self.transform = CGAffineTransformIdentity;
moved = NO;
zoomed = NO;
}
return;
}
}
else {
// multi-touch
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
CGFloat finalDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self]
toPoint:[touch2 locationInView:self]];
CGFloat movement = finalDistance - initialDistance;
NSLog(#"Final Distance: %f, movement=%f",finalDistance,movement);
if (movement != 0) {
CGFloat scale = [self scaleAmount: movement];
self.transform = CGAffineTransformScale(self.transform, scale, scale);
NSLog(#"Scaling: %f", scale);
zoomed = YES;
}
}
}
- (void)singleTap: (NSTimer*)theTimer {
// must override
}
- (void)animateSwipe: (int) direction {
// must override
}
It is not working fine on device. can anyone tell that where i am wrong.
When you use any CGAffinTransform.... the value of the frame property becomes undefined. In your code you are using the frame.size.width and frame.size.height to calculate the change in size. After the first iteration of CGAffinTransformScale you would not get the right scale factor. According to the documentation bounds would be the right property for scale calculations.