小编Mag*_*ave的帖子

比较两个NSDates并忽略时间组件

比较两个NSDates的最有效/推荐方法是什么?我希望能够看到两个日期是否在同一天,无论时间如何,并且已经开始编写一些在NSDate类中使用timeIntervalSinceDate:方法的代码,并获取此值的整数除以秒数一天内.这似乎很长,我觉得我错过了一些明显的东西.

我试图解决的代码是:

if (!([key compare:todaysDate] == NSOrderedDescending))
{
    todaysDateSection = [eventSectionsArray count] - 1;
}
Run Code Online (Sandbox Code Playgroud)

其中key和todaysDate是NSDate对象,todaysDate正在使用:

NSDate *todaysDate = [[NSDate alloc] init];
Run Code Online (Sandbox Code Playgroud)

问候

戴夫

iphone objective-c nsdate

71
推荐指数
7
解决办法
4万
查看次数

通过触摸到下面的UIViews

我有一个UIView4个按钮,另一个UIView在按钮视图的顶部.最顶层的视图包含UIImageView一个UITapGestureRecognizeron.

我试图创建的行为是,当用户点击UIImageView它时,它在屏幕右下角的小视图和动画变大之间切换.当它很大时,我想要禁用底部视图上的按钮,当它很小时,在右下角我希望触摸传递到按钮并让它们正常工作.我几乎在那里,但除非我禁用UserInteractions顶视图,否则无法通过按钮进行操作.

initWithFrame:在顶视图中有这个:

// Add a gesture recognizer to the image view
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
tapGestureRecognizer.cancelsTouchesInView = NO;
[imageView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
Run Code Online (Sandbox Code Playgroud)

我这是我的imageTapped:方法:

- (void) imageTapped:(UITapGestureRecognizer *) gestureRecognizer {
    // Toggle between expanding and contracting the image
    if (expanded) {
        [self contractAnimated:YES];
        expanded = NO;
        gestureRecognizer.cancelsTouchesInView = NO;
        self.userInteractionEnabled = NO;
        self.exclusiveTouch = NO;
    }
    else {
        [self expandAnimated:YES];
        expanded …
Run Code Online (Sandbox Code Playgroud)

iphone uiview uiimageview ios uitapgesturerecognizer

64
推荐指数
3
解决办法
6万
查看次数

如何确定应用程序包中是否存在文件?

对不起,今天愚蠢的问题2.是否可以确定App Bundle中是否包含文件?我可以访问文件没问题,即

NSString *pathAndFileName = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];
Run Code Online (Sandbox Code Playgroud)

但无法弄清楚如何检查文件是否存在于那里.

问候

戴夫

iphone nsbundle

49
推荐指数
3
解决办法
3万
查看次数

调整WhenFontSizeToFitWidth时剪切UILabel

当我将文本添加到标签时,文本的adjustsFontSizeToFitWidth设置YES不再垂直居中,最终剪切标签框底部的文本.对于大量文本,它最终会从标签的底部消失.

看法背景是蓝色的,标签背景是白色的

如果您添加较少的文本会发生这种情况:

在此输入图像描述

这是我所期望的剪裁(即字体大小没有缩小,文字在标签中垂直居中,并在顶部和底部剪裁.

在此输入图像描述

这是重现的代码:

- (void)loadView {
    [super loadView];

    self.view.backgroundColor = [UIColor blueColor];
    testLabel = [[UILabel alloc] init];
    testLabel.font = [UIFont boldSystemFontOfSize:172];
    testLabel.textColor = [UIColor blackColor];
    testLabel.adjustsFontSizeToFitWidth = YES;
    testLabel.numberOfLines = 1;
    testLabel.frame = CGRectMake(50, 50, 300, 100);
    testLabel.text = @"123";

    [self.view addSubview:testLabel];
}
Run Code Online (Sandbox Code Playgroud)

这会发生吗?如何使我的标签垂直居中,而不管标签中的字符数是多少.

clipping uilabel ios

24
推荐指数
1
解决办法
6240
查看次数

以编程方式确定UIView的最大可用帧大小

我已经看到了几个类似的问题,但没有一个能满足我的特殊需求.我希望能够编写一个通用的辅助方法,它返回UIView的最大可用帧大小,考虑到应用程序是否具有状态栏,导航栏和/或标签栏的任意组合,因为我发现自己这样做了时间.

方法定义将作为UIScreen的扩展:

+ (CGRect) maximumUsableFrame;
Run Code Online (Sandbox Code Playgroud)

可以从中获取带或不带状态栏的大小

[UIScreen mainScreen].applicationFrame

属性,但我无法找出一种方法来确定是否存在导航栏或标签栏.我已经考虑过在我的app委托中维护一些全局标志,但这看起来非常笨重,并且停止了代码的通用性和可重用性.我还考虑将UIView作为参数传递,获取视图的窗口,然后是rootViewController,然后查看是否设置了导航控制器属性.如果是,则检查导航控制器是否隐藏.如果你问我,一切都很笨重.

任何想法将不胜感激.

戴夫

编辑:结合Caleb答案中的想法,以防其他人使用:

// Extension to UIViewController to return the maxiumum usable frame size for a view
@implementation UIViewController (SCLibrary)

- (CGRect) maximumUsableFrame {

    static CGFloat const kNavigationBarPortraitHeight = 44;
    static CGFloat const kNavigationBarLandscapeHeight = 34;
    static CGFloat const kToolBarHeight = 49;

    // Start with the screen size minus the status bar if present
    CGRect maxFrame = [UIScreen mainScreen].applicationFrame;

    // If the orientation is landscape left or landscape right then swap …
Run Code Online (Sandbox Code Playgroud)

cocoa-touch uiviewcontroller uiview ios

22
推荐指数
2
解决办法
1万
查看次数

如果按下子视图按钮,如何取消UIGestureRecognizer

我正在努力从手势识别器中获取我想要的行为,特别是如果其他人已经解雇则取消某些手势.

我有一个scrollView设置为分页和每个页面中的多个子视图.如果用户点击页面的右侧或左侧,我已添加了触摸手势识别器以滚动到下一页或上一页.

    // Add a gesture recogniser turn pages on a single tap at the edge of a page
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    tapGesture.cancelsTouchesInView = NO;
    [self addGestureRecognizer:tapGesture];
    [tapGesture release];
Run Code Online (Sandbox Code Playgroud)

和我的手势处理程序:

- (void) tapGestureHandler:(UIGestureRecognizer *) gestureRecognizer {
    const CGFloat kTapMargin = 180;

    // Get the position of the point tapped in the window co-ordinate system
    CGPoint tapPoint = [gestureRecognizer locationInView:nil];

    // If the tap point is to the left of the page then go back a page …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch uibutton uigesturerecognizer

17
推荐指数
2
解决办法
1万
查看次数

从旋转的UIImageView创建UIImage

我有一个UIImageView,里面有一个图像.我通过将UIImageView的transform属性设置为CGAffineTransformMakeRotation(angle),在显示之前旋转了图像,其中angle是以弧度表示的角度.

我希望能够创建另一个UIImage,它对应于我在视图中可以看到的旋转版本.

我几乎在那里,通过旋转图像上下文我得到一个旋转的图像:

- (UIImage *) rotatedImageFromImageView: (UIImageView *) imageView
{
    UIImage *rotatedImage;

    // Get image width, height of the bounding rectangle
    CGRect boundingRect = [self getBoundingRectAfterRotation: imageView.bounds byAngle:angle];

    // Create a graphics context the size of the bounding rectangle
    UIGraphicsBeginImageContext(boundingRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Rotate and translate the context
    CGAffineTransform ourTransform = CGAffineTransformIdentity;
    ourTransform = CGAffineTransformConcat(ourTransform, CGAffineTransformMakeRotation(angle));

    CGContextConcatCTM(context, ourTransform);

    // Draw the image into the context
    CGContextDrawImage(context, CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage);

    // Get an image from the …
Run Code Online (Sandbox Code Playgroud)

rotation uiimageview uiimage graphicscontext

15
推荐指数
1
解决办法
1万
查看次数

以编程方式在Xcode中按下UITabBar按钮

对不起新手问题.我在主窗口视图中有一个UITabBar,每个Tab都有一个UINavigationControllers数组.该结构类似于iPod应用程序,通过选择TabBar项目可以看到主视图,然后用户可以通过将视图推送到堆栈来进一步向下钻取NavigationController.

我希望能够做的就是在代码中的任何子视图底部按下TabBar按钮(即,更改TabBar的选定属性并显示启动选项卡的第一个视图控制器) .

任何帮助将不胜感激.

戴夫

iphone cocoa-touch objective-c uinavigationcontroller uitabbar

12
推荐指数
2
解决办法
3万
查看次数

setMessageBody中忽略了新行和返回

我在做一些蠢事吗?我可以预先填写并发送电子邮件,但在电子邮件中忽略"\ r \n":

- (void) sendEventInEmail
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    NSString *emailSubject = [eventDictionary objectForKey:EVENT_NAME_KEY];

    [picker setSubject:emailSubject];

    // Fill out the email body text
    NSString *iTunesLink = @"http://itunes.apple.com/gb/app/whats-on-reading/id347859140?mt=8"; // Link to iTune App link
    NSString *content = [eventDictionary objectForKey:@"Description"];
    NSString *emailBody = [NSString stringWithFormat:@"%@\r\nSent using <a href = '%@'>What's On Reading</a> for the iPhone.", content, iTunesLink];

    [picker setMessageBody:emailBody isHTML:YES];

    picker.navigationBar.barStyle = UIBarStyleBlack;

    [self presentModalViewController:picker animated:YES];
    [picker release];
}
Run Code Online (Sandbox Code Playgroud)

问候

戴夫

iphone mfmailcomposeviewcontroller

12
推荐指数
2
解决办法
5967
查看次数

转船的物理特性

我正在写一个小船模拟(一艘帆船在动力而不是帆船下).它有一个方向舵和一个龙骨,我的大部分物理都是为推力和阻力而工作.我已经模拟了横跨船的阻力高于沿船的阻力以减少侧向滑动.

然而,它表现得并不正确.我是否正确地说龙骨不仅会提供抵抗力,还会通过引水来帮助船只通过转弯?

最终的模型不一定具有科学的准确性,只需要像船一样"感觉".

任何指针都将非常感激.

最好的问候和提前感谢.

戴夫


编辑(最初由OP发布的答案)

好吧,意识到我错过了龙骨产生的水动力"升力".看起来它像水下航行一样,产生一个力矢量,与它上面的水流量和流量的角度成比例.力矢量与水穿过龙骨并朝向船指向的方向相反.理论上,这应该减少侧向滑动并且还将船拉过转弯.

如果有人知道这些东西,如果他们可以添加一个能够理解我的基本理解的答案,那将非常感激.我试图将其提炼成一个与船指向的方向以及船的行进方向和速度有关的公式.


好吧,意识到我错过了龙骨产生的水动力"升力".看起来它像水下航行一样,产生一个力矢量,与它上面的水流量和流量的角度成比例.力矢量与水穿过龙骨并朝向船指向的方向相反.理论上,这应该减少侧向滑动并且还将船拉过转弯.

如果有人知道这些东西,如果他们可以添加一个能够理解我的基本理解的答案,那将非常感激.我试图将其提炼成一个与船指向的方向以及船的行进方向和速度有关的公式.

physics game-physics

12
推荐指数
1
解决办法
2544
查看次数